Introduction
In this article we will continue from our last article Paint Application in Silverlight 3 - Part 1. In our previous article we designed the whole Silverlight Application. Now we will continue.
Drawing Logic
Basically while drawing with the mouse, we will create lines. So we need two Points. As you will see in my code I have used two Points; as a line requires two points to be drawn. I have taken a Boolean value bool MouseDraw = false; based on the value we will draw.
Now we need some events to be triggered when we will draw. We are planning to draw on a grid called DrawingArea. So we need three events from the Grid. Such as: LeftMouseButtonDown, LeftMouseButtonUp and MouseMove.
The following code will guide you through.
private void DrawingArea_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MouseDraw = false;
}
private void DrawingArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
pt = e.GetPosition(DrawingArea);
ptOld1 = pt;
MouseDraw = true;
}
private void DrawingArea_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDraw)
{
pt = e.GetPosition(DrawingArea);
Line1 = new Line();
Line1.X1 = pt.X;
Line1.Y1 = pt.Y;
Line1.X2 = ptOld1.X;
Line1.Y2 = ptOld1.Y;
Line1.Stroke = rectFront.Fill;
DrawShapeType();
Line1.StrokeThickness = BrushSize.Value;
Line1.Opacity = BrushOpacity.Value;
DrawingArea.Children.Add(Line1);
ptOld1 = pt;
}
ptOld1 = pt;
}
Collecting Draw Color Information
You have seen that I have assigned the color of the Line from a Rectangle's Fill property. Now as I have explained you in my earlier article that I created 28 Colors based on the combination. So to make the draw Color easy to choose; I have used another Rectangle and on each Rectangle's Click Property I am setting the Color to the RectFront Rectangle's Fill.
The following code will guide you through.
#region Rectangle Colors
private void rect0_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect0.Fill; }
private void rect1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect1.Fill; }
private void rect2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect2.Fill; }
private void rect3_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect3.Fill; }
private void rect4_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect4.Fill; }
private void rect5_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect5.Fill; }
private void rect6_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect6.Fill; }
private void rect7_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect7.Fill; }
private void rect8_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect8.Fill; }
private void rect9_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect9.Fill; }
private void rect10_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{ rectFront.Fill = rect10.Fill; }

jay davidPosted Apr 4, 2017, 6:51 AM
THANK YOU SO MUCH!!! You are a Godsend. This helped with my project
Gowtham RajamanickamPosted May 20, 2015, 12:58 AM
helpful
reenaPosted Aug 6, 2014, 1:54 AM
Thanks for the help your article was extremely helpful...I'm also developing the same paint application in silverlight and want to save it in a db...can you help me with this???
reenaPosted Aug 6, 2014, 1:52 AM
hi
studioPosted Jan 19, 2012, 4:28 AM
Got an error on old version of silverlight
vix cPosted Feb 20, 2010, 1:54 PM
Just a quick question where does all this code on this page go into exactly? I'm unsure where to put it??