Basic Drawing and Shapes in Windows Presentation Foundation

This article covers the new features introduced in Windows Presentation Foundation for drawing shapes. A Shape is a type of UIElement, and enables you to draw a shape to the screen. The Content Panels can contain Shape elements as children. Whenever the properties of these elements got changed, screen reflects those changes automatically.


Shape Objects

WPF provides number of Shape classes. These are derived from Shape class.
Ellipse, Line, Path, Polygon, Polyline, and Rectangle are few classes derived from Shape class. The WPF also defines 3DGementric shapes. The user can define any 3D shape by specifying collection triangular surfaces in 3D space.

Shape class has following properties:

Stroke - StrokeThickness - Fill - Data

->> Stretch : Specify the behavior of the shapes, when the width and height of Parent element got changed. The following are values
'Stretch' property : None - Fill - Uniform - UniformToFill

->> Transformation : Specify the type and measure for transformation

RotateTransform - Centre, Angle
ScaleTransform - Origin, scale factor
SkewTransform - Tilting angle
TranslateTransform - Displacement in x and y

Example:

The example below defines a line and polyline elements with a stretch to fill and 10 deg rotation.

<StackPanel>
<Line
X1="10" Y1="10"
X2="50" Y2="50"
Stroke="Black"
StrokeThickness="4" />
<Polyline
Fill="Blue"
Points="100,100 100,200 200,200 200,100"
Stroke="Red"
StrokeThickness="10"
Stretch="Fill"
RenderTransformOrigin="0.5,0.5" >
<Polyline.RenderTransform>
<RotateTransform Angle="10" />
</Polyline.RenderTransform>
</Polyline>

</StackPanel>

The result of the above code will give you an image as shown below:

Thus you can use the shape object to draw the shapes. You can access the different graphics and rendering services that are available in the Windows Presentation Foundation (WPF).


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.