Search
Author
ASP.NET Tutorials
Author
Sheo Narayan
Winners

Win Prizes

Social Presence
Like us on Facebook

Silverlight Tutorials | Report a Bug in the Tutorial
Silverlight Drawing Tutorials
Silverlight supports basic vector graphics like Rectangle, Ellipse, Polyline, Polygon, Line elements. Generally these elements are used in drawing and paintings.
 
DEMO : Drawing objects Show Source Code
Following code demonstrate the basic commonly used elements that are supported by Silverlight. These are Ellipse, Rectangle and Line elements.
    <Canvas 
    xmlns="http://schemas.microsoft.com/client/2007" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Ellipse Height="50" Width="50" Canvas.Left="5" Canvas.Top="5" 
        Fill="Red" Stroke="Black" StrokeThickness="2"></Ellipse>
      <Rectangle Height="50" Width="150" Canvas.Left="25" 
        Canvas.Top="25" Fill="Yellow" Stroke="Blue" StrokeThickness="3"></Rectangle>
      <Line X1="180" Y1="10" X2="10" Y2="60" Stroke="Maroon" StrokeThickness="5"></Line> 
  </Canvas>
                    
Apart from Ellipse, Rectangle and Line, Silverlight also supports Polyline, Polygon and Path elements. Path element is exciting element that is used to draw complex shapes.
<Canvas 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Polygon Points="10, 10, 10, 100, 100, 100, 100, 10" 
    Fill="#dedede" Stroke="Black" StrokeThickness="2"></Polygon>
    <Polyline Points="105, 105, 105, 145, 145, 145, 145, 50, 5, 150, 185, 185" 
    Stroke="Red" StrokeThickness="3"></Polyline>
  </Canvas>
                    
As written earlier, Path element is used to draw complexx type of shaps. To use Path element we need to set a special type of attribute called Data. The Data attribute string contains several single character string and all have different meaning.
M denotes the start point in the absolute value
m denotes the start point in an offset to the previous point
L denotes the stright line between the current line and the specified point
H denotes the horizontal line between the current point and the specified x-cordinate
V denotes the vertical line between the current point and the specified y-cordinate
C denotes a cubic Bezier curve between the current point and specified end point by using the two specified control points
Q denotes the quadratic Bezier curve point between the current point and the specified end point by using a specified control point
S denotes a cubit Bezier curve between the current point and specified end point by using the two specific control points, here the first control point is assumed to be the reflection of the second control point
A denotes the elliptical arc between the current point and the specified end point
Z denotes the end of the current shape and creates the line that connect the current point to the starting point.
<Canvas 
    xmlns="http://schemas.microsoft.com/client/2007" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Path Data="M 10,20 C 10,200 200,-100 175,100Z" 
    Stroke="Orange" Fill="Pink" Canvas.Left="10" Canvas.Top="10" />
    <Path Data="M 10, 150, 30, 150, 30, 300, 20, 350, 10, 300 Z" 
    Stroke="Blue" Fill="LightBlue" Canvas.Left="5" Canvas.Top="5" />
    <Path Data="M 50, 150, 175, 375, 50, 375, 175, 150 Z" 
    Stroke="Red" Fill="Yellow" Canvas.Left="5" Canvas.Top="5" />
  </Canvas>