Programmatic drawing with Silverlight Part 2: C#
Posted by Dennis on Mar 3, 2008 in Silverlight • 6 commentsIn one of my previous posts I explored how programmatic drawing with Silverlight 1.0/JavaScript worked. It was done by constructing a XAML string and inserting it into the XAML hierarchy. Not a nice way of doing things, imho.
Fortunately Silverlight 1.1 alpha offers the possibility to use C#. This way there’s no need to construct strings anymore because the drawing API can be used for drawing. This is the same code I used in my previous post, but ported to C#:
Note Some things have changed in Silverlight 2 Beta 1:
- [Scriptable] on methods changed to [ScriptableMember()]
- Removed [Scriptable] on the class
- Changed the managed code scriptable object registration from WebApplication.Current.RegisterScriptableObject(”basic”, this);
toHtmlPage.RegisterScriptableObject(”basic”, this);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net;
namespace Whirl2
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
HtmlPage.RegisterScriptableObject("basic", this);
drawWhirl(3);
}
[ScriptableMember()]
public void drawWhirl(uint numSides)
{
double[] x = new double[numSides + 1];
double[] y = new double[numSides + 1];
DateTime startTime = DateTime.Now;
uint scaleFactor = 275;
uint centerX = 300;
uint centerY = 300;
uint numLinesDrawn = 0;
double rotAngle = 0.05;
double rotAngleSin = Math.Sin(rotAngle);
double rotAngleCos = Math.Cos(rotAngle);
double a = (Math.PI * (1 - 2 / (double)numSides));
double c = Math.Sin(a) / (rotAngleSin + Math.Sin(a + rotAngle));
Canvas layoutRoot = FindName("LayoutRoot") as Canvas;
layoutRoot.Children.Clear();
for (uint k = 0; k <= numSides; k++)
{
double t = (2 * k + 1) * Math.PI / (double)numSides;
x[k] = Math.Sin(t);
y[k] = Math.Cos(t);
}
for (uint n = 1; n < 64; n++)
{
Point previousPoint = new Point();
previousPoint.X = (x[0] * scaleFactor) + centerX;
previousPoint.Y = (y[0] * scaleFactor) + centerY;
for (uint l = 0; l <= numSides; l++)
{
Point newPoint = new Point();
newPoint.X = (x[l] * scaleFactor) + centerX;
newPoint.Y = (y[l] * scaleFactor) + centerY;
Line line = new Line();
line.X1 = previousPoint.X;
line.Y1 = previousPoint.Y;
line.X2 = newPoint.X;
line.Y2 = newPoint.Y;
line.StrokeThickness = 1;
SolidColorBrush stroke = new SolidColorBrush();
stroke.Color = Colors.Black;
line.Stroke = stroke;
layoutRoot.Children.Add(line);
numLinesDrawn++;
previousPoint = newPoint;
}
for (uint m = 0; m <= numSides; m++)
{
double z = x[m];
x[m] = (x[m] * rotAngleCos - y[m] * rotAngleSin) * c;
y[m] = (z * rotAngleSin + y[m] * rotAngleCos) * c;
}
}
HtmlElement divInfo = HtmlPage.Document.GetElementById("divInfo");
divInfo.SetAttribute("innerHTML",
"<strong>Number of sides:</strong> " + numSides + "<br>" +
"<strong>Number of lines drawn:</strong> " + numLinesDrawn + "<br>" +
"<strong>Time:</strong> " + ((DateTime.Now - startTime).Milliseconds) + " milliseconds");
}
}
}
Take a look and compare the performance to the JavaScript version. Not surprisingly, it’s much faster.




Awesome Post, will difinatly use this at some point. Thanks
I’m very thankful to the author for posting such an amazing web design development post. Continuing to the post, I want to add some interesting updates, UK’s leading web development entity Rupizmedia has introduced some exciting web applications like live TV applications, CMS for Dynamic Shopping Website, Video Conferencing applications, Community softwares and more than hundreds of much awaited web products & softwares to boost any e-business in a more fruitful way. If you want to know more about these ultimate products, which give a strong interactive approach to your online business, Visit the most popular Website Development firm over the globe.
Hi I am a general user of Adobe Flash, and would like to know if there is any advantage in switching over to Silverlight?
Can you also post an article on writing storyboard, how can we write a good storyboard…
i saw some web sites done with c# silverlight and i dont think in close future it can get closer to flash…
Dennis?! @#$$%@ van je geloof afgevallen? Of probeer je ‘t uit om het daarna af te schieten?
Jan