Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / WPF
Tip/Trick

Shapes and Custom Drawing in WPF/Silverlight

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
7 Mar 2013CPOL2 min read 31.8K   8   1
In this article I will tell you how to create shapes and create custom drawings using the Path property.

Introduction 

In this article I will tell you how to create shapes and create a custom drawing using the Path property.

Using the code

There are three common properties for shapes, listed below:

  1. Fill: This property is used to fill shape with color.
  2. Stroke: This property describes border color for the shape.
  3. StokeThickness: This property set shape border thickness.

Now we will discuss the various shapes into details.

1. Line

The Line element of XAML draws a line between two points.

There are four attributes to draw a line, (X1,Y1,X2,Y2) represents the start point and end point for the X and Y co-ordinates of a line:

Image 1

XML
<Line Stroke="#000fff" StrokeThickness="2" X1="100"Y1="100" X2="300" Y2="100" />

2. Rectangle

The Rectangle element of XAML draws a rectangle.

Image 2

XML
<Rectangle Width="300" Height="100" Fill="Black" Stroke="red" StrokeThickness="5">
</Rectangle>
    <Rectangle Width="120" Height="100" RadiusX="40" RadiusY="40" Stroke="Green" StrokeThickness="5" Fill="Black">
</Rectangle>

In the above figure, first is a simple rectangle and the second figure indicates a rounded rectangle. For drawing a rounded rectangle we have to set two more properties: RadiusX and RadiusY for a round corner.

Ellipse

This shape creates an ellipse to a circle in the specified width and height.

Image 3

XML
<Ellipse Height="100" Width="300" StrokeThickness="0.4" 
   Opacity="0.5" Stroke="Black"  Fill="Gold"/>

Polyline

This shape is used draw a series of connected straight lines.

Image 4

XML
<Polyline Points="0,50 20,0 40,50 60,0 80,50 100,0 120,50 -4,50"
                      Stroke="Black"
                      StrokeThickness="10"
                      Canvas.Left="75"
                      Canvas.Top="50" />

Polygon

This shape is used for drawing a polygon, which is a connected series of lines that form a closed shape. The following example shows how to create different types of shapes using a polygon.

Example

Image 5

XML
<Polygon Points="100,0 75,75 100,100 125,75"
                 Stroke="Black" StrokeThickness="2" Fill="AliceBlue"/> 
<Polygon Points="100,100 125,125 100,200 75,125"
                 Stroke="Yellow" StrokeThickness="2" Fill="Black"/>
<Polygon Points="100,100 125,75 200,100 125,125"
                 Stroke="Red" StrokeThickness="2" Fill="Azure"/>
<Polygon Points="100,100 75,125 0,100 75,75"
                 Stroke="Blue" StrokeThickness="2" Fill="BlanchedAlmond"/>

Custom shape using Path

Now I will tell you how to create custom shapes using Path and the Data property of the Path element. The Path class is used to draw curves and complex shapes. It is described using the Geometry object. There are a various types of Geometry objects that describe shapes: LineGeometry, RectangleGeometry, and EllipseGeometry.

Example 1: Draw Triangle

Image 6

XML
<Path Data="M50.0000011012083,0.5L99.5000021617096,99.5L0.500000040707015,99.5z" Stroke="Black"
  StrokeThickness="4" Fill="Yellow"/>

Example 2: Draw Arc

Image 7

XML
<Path Data="M25,0.5C38.5309753417969,0.5,49.5,9.23044681549072,49.5,20" Stroke="Black"
 StrokeThickness="5" Fill="Yellow" Margin="0,33,0,0"/>

Example 3: Draw Arrow

Image 8

XML
<Path Data="M20.125,32L0.5,12.375L10.3125,12.375L10.3125,0.5L29.9375,0.5L29.9375,12.375L39.75,12.375z" 
  Stroke="Black" StrokeThickness="5" Fill="Yellow" Margin="162,33,0,0" />

In the above example we gave the path data from Microsoft Expression Blend.

Happy programming!!

Don’t forget to leave your feedback and comments below!

If you have any queries, mail me at Sujeet.bhujbal@gmail.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Sujit Bhujbal is Senior Software Engineer having over 12 + years of Experience in .NET core , C#, Angular , SQL Server and has worked on various platforms. He worked during various phases of SDLC such as Requirements and Analysis, Design and Construction, development, maintenance, Testing, UAT.

He is Microsoft Certified Technology Specialist (MCTS) in Asp.net /WCF Applications. He worked at various levels and currently working as a Senior Software Engineer.

His core areas of skill are Web application development using WPF,WCF , C#.Net, ASP.Net 3.5, WCF, SQL Server 2008, CSS, Java script Web design using HTML, AJAX and Crystal Reports

He is proficient in developing applications using SilverLight irrespective of the language, with sound knowledge of Microsoft Expression Blend and exposure to other Microsoft Expression studio tools.


Microsoft Certified Technology Specialist (MCTS): Web Applications Development with Microsoft .NET Framework 4
Microsoft Certified Technology Specialist (MCTS): Accessing Data with Microsoft .NET Framework 4
Microsoft Certified Technology Specialist (MCTS): Windows Communication Foundation Development with Microsoft .NET Framework 4

------------------------------------------------------------------------
Blog: Visit Sujit Bhujbal

CodeProject:- Sujit Bhujbal codeproject

DotNetHeaven:- DotNetHeaven

CsharpCorner:-CsharpCorner

Linkedin :-Linkedin

Stack-Exchange: <a href="http://stackexchange.com/users/469811/sujit-bhu

Comments and Discussions

 
QuestionPlease let me know how to create path for curlybrace shape Pin
sateesh munagala20-Feb-14 17:57
sateesh munagala20-Feb-14 17:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.