A graphics path is a set of connected lines, curves, and other simple graphics objects, including rectangles, ellipses, and text. This article demonstrates how to create and use paths in WPF and XAML.

Introduction

A graphics path is a set of connected lines, curves, and other simple graphics objects, including rectangles, ellipses, and text. A path works as a single graphics object, so an effect applied to the graphics path will be applied to all the components of the path. For example, if a graphics path contains a line, a rectangle, and an ellipse and we draw the path using a red stroke, all three components (line, rectangle, and ellipse) of the graphics path will be drawn with the red stroke.

The Path object represents a path shape and draws a path. The Path object draws both closed and open paths. A closed path is a shape that has the same start and end points and an open path is a shape that has different start and end points.

The Fill property fills the interior of an ellipse. The Stroke property sets the color and StrokeThickness represents the width of the outer line of an ellipse.

The Data property of the Path object defines a shape or a collection of shapes in form of Geometry.

The following code snippet creates an arc shape using a path.

1. <Path Stroke="Black" StrokeThickness="4" Data="M 80,200 A 100,50 45 1 0 100,50" />

The output looks like Figure 8.

Figure 8

Before I discuss paths any further, we need to understand the Geometry class, its members, and its related classes.

Understanding Geometry

The Geometry class that defines the geometry of a shape plays a vital role in creating paths. This class cannot be used directly but used in the forms of its derived classes LineGeometry, RectangleGeometry, EllipseGeometry, GroupGeometry, PathGeometry, CombinedGeometry, and StreamGeometry. These geometry objects can be used for clipping, hit-testing, and rendering complex shapes.

The LineGeometry class represents the geometry of a line. The StartPoint and EndPoint properties of the LineGeometry class define the start and end points of a line. The following code snippet creates geometry of a line.

1. <LineGeometry StartPoint="20,50" EndPoint="200,50" />

The RectangleGeometry class represents the geometry of a rectangle. The Rect property of the RectangleGeomerty defines the starting points, width, and height of a rectangle. The following code snippet creates geometry of a rectangle.

1. <RectangleGeometry Rect="80,167 150 30"/>

The EllipseGeometry class represents the geometry of an ellipse. The Center property of the EllipseGeomerty defines the center of an ellipse. The RadiusX and RadiusY define the width and height of an ellipse. The following code snippet creates geometry of an ellipse.

The GeometryGroup is creates a composite geometry that is a combination of multiple Geometry objects.

The code listed in Listing 8 creates a GeometryGroup with three geometry shapes – a line, an ellipse, and a rectangle and sets the Data property of a path.

1. <Path Stroke="Black" StrokeThickness="3" Fill="Blue">
2.     <Path.Data>
3.         <GeometryGroup>
4.             <LineGeometry StartPoint="20,200" EndPoint="300,200" />
6.             <RectangleGeometry Rect="80,167 150 30" />
7.         </GeometryGroup>
8.     </Path.Data>
9. </Path>

Listing 8

The output of Listing 8 looks like Figure 9.

Figure 9. A composite shape

The FillRule property of the GeometryGroup class specifies how the intersecting areas of geometry objects in a GeometryGroup are combined. It has two values – EvenOdd and NonZero. The default value of the FillRule is EvenOdd. In this case, the intersecting area of two shapes is not filled. In case of NonZero, the interesting area of two shapes is filled. By setting the FillRule to NonZero generates Figure 10.

Figure 10. A composite shape with NonZero FillRule

The code listed in Listing 9 creates Figure 9 dynamically. As you can see from Listing 9, we create a LineGeometry, an EllipseGeometry, and a RectangleGeometry and then we create a GroupGeometry and add all three geometries to the GroupGeometry. After that, we simply set the Data property of Path to the GroupGeometry.

1. ///<summary>
2. /// Creates a blue path with black stroke
3. ///</summary>
4. publicvoid CreateAPath() {
5.     // Create a blue and a black Brush
6.     SolidColorBrush blueBrush = newSolidColorBrush();
7.     blueBrush.Color = Colors.Blue;
8.     SolidColorBrush blackBrush = newSolidColorBrush();
9.     blackBrush.Color = Colors.Black;
10.     // Create a Path with black brush and blue fill
11.     Path bluePath = newPath();
12.     bluePath.Stroke = blackBrush;
13.     bluePath.StrokeThickness = 3;
14.     bluePath.Fill = blueBrush;
15.     // Create a line geometry
16.     LineGeometry blackLineGeometry = newLineGeometry();
17.     blackLineGeometry.StartPoint = newPoint(20, 200);
18.     blackLineGeometry.EndPoint = newPoint(300, 200);
19.     // Create an ellipse geometry
20.     EllipseGeometry blackEllipseGeometry = newEllipseGeometry();
21.     blackEllipseGeometry.Center = newPoint(80, 150);
24.     // Create a rectangle geometry
25.     RectangleGeometry blackRectGeometry = newRectangleGeometry();
26.     Rect rct = newRect();
27.     rct.X = 80;
28.     rct.Y = 167;
29.     rct.Width = 150;
30.     rct.Height = 30;
31.     blackRectGeometry.Rect = rct;
32.     // Add all the geometries to a GeometryGroup.
33.     GeometryGroup blueGeometryGroup = newGeometryGroup();