Shapes

This section covers the following topics:

Primitives

Line

To draw a line, use the DrawLine() method. The arguments to this method are the X and Y coordinates of the start and end point of the line. The line is considered to exist by itself; drawing two lines does not join the lines together. To draw shapes composed of multiple lines, see the Paths section below.

g.DrawLine(Rgb.BluePen, 20, 20, 500, 20);

Rectangle

To draw a rectangle, use the DrawRectangle() method. To fill a rectangle, use FillRectangle(). You can either specify the rectangle's position and dimensions separately, or use a Rectangle value.

g.DrawRectangle(Rgb.BluePen, 20, 20, 100, 50); g.FillRectangle(Rgb.GreenBrush, new Rectangle( 20, 20, 100, 50));

Rounded rectangle

A rounded rectangle is a rectangle with rounded corners. Use the DrawRoundedRectangle() and FillRoundedRectangle() methods to draw or fill, respectively. Both methods accept a radius as the final argument. The radius determines the size of the roundings.

g.FillRoundedRectangle(Rgb.RedBrush, 20, 20, 100, 50, 3);

Circles and ellipses

Circles and ellipses are painted with the DrawEllipse() and FillEllipse() methods. These methods accept a Rectangle argument that serves as the bounding box of the ellipse. To draw or fill a circle, specify a bounding box with equal width and height.

g.DrawEllipse(Rgb.BluePen, new Rectangle(20, 20, 100, 50)); g.FillEllipse(Rgb.GreenBrush, new Rectangle(200, 20, 80, 80));

In the above example, line 1 draws an ellipse outline with a horizontal diameter of 100 points and a vertical diameter of 50 points. Line 2 fills a circle with a diameter of 80 points.

Note: The PDF specification does not include ellipses or circles. Instead, PDF Graphics .NET simulates these shapes through bezier curves. In practice, the simulation is adequate and ellipses and circles are painted accurately.

Paths

It is possible to construct more elaborate shapes, including ones that use curves instead of straight lines, using the GraphicsPath class. To paint a path, it needs to be defined first. After that, it can be drawn or filled.

To start defining a path, construct an instance of GraphicsPath. Then, use its methods to add lines, curves and rectangles. See the example below:

GraphicsPath path = new GraphicsPath(); path.AddLine(0, 0, 100, 0); path.AddLine(100, 0, 100, 100); path.AddLine(100, 100, 50, 150); path.AddLine(50, 150, 0, 100); path.Close(); g.DrawPath(Rgb.RedPen, path, 50, 50);

In the example, a path is constructed and three lines added to it. The path is then closed. This will cause a line to be added that links the first and last point in the path. The path is then drawn with a red pen. If the shape hadn't been closed, the shape would have a gap between the first and last points. Compare the two results below:

Open paths have no effect when filling a path with a brush. The resulting shape is the same for an open path and a closed one.

The GraphicsPath class has methods for adding lines, curves and rectangles.

AddLine()
Adds a line to the path. The end point of this line will be remembered as the path's last point. If the start point or end point of the line is the same as the last point in the path, the new line is joined to the last point, and the appearance of the join is affected by the pen used to draw the path with (see Colours, pens and brushes).

AddCurve()
Adds a cubic bezier curve to the path. The end point of this curve will be remembered as the path's last point. If the start point or end point of the curve is the same as the last point in the path, the curve is joined to the last point, and the appearance of the join is affected by the pen used to draw the path with.

AddRectangle()
Adds a rectangle to the path. This resets the path's last point, so that the next line or curve drawn will not be joined to the previous last point.

AddRoundedRectangle()
Adds a rounded rectangle to the path. This resets the path's last point, so that the next line or curve drawn will not be joined to the previous last point.

Multiple shapes

A single path can contain multiple shapes, each composed of a set of elements. The different shapes are distinguished from each other by closing the current set of elements before adding new elements. The GraphicsPath's Close() method doubles as a closing operator and shape separator. Take a look at the following example:

GraphicsPath twoShapes = new GraphicsPath(); Rectangle rect = new Rectangle(0, 0, 50, 50); twoShapes.AddLine( 0, rect.Height / 2f, rect.Width / 2f, 0); twoShapes.AddLine( rect.Width/2f, 0, rect.Width/2f, rect.Height/4f); twoShapes.AddLine( rect.Width / 2f, rect.Height / 4f, rect.Width, rect.Height / 4f); twoShapes.AddLine( rect.Width, rect.Height / 4f, rect.Width, rect.Height * 3 / 4f); twoShapes.AddLine( rect.Width, rect.Height * 3 / 4f, rect.Width/2f, rect.Height * 3 / 4f); twoShapes.AddLine( rect.Width / 2f, rect.Height * 3 / 4f, rect.Width / 2f, rect.Height); twoShapes.Close(); rect.Move(rect.Width * 2.2f, 0); twoShapes.AddLine( rect.Left, rect.Height / 2f, rect.Left - rect.Width / 2f, 0); twoShapes.AddLine( rect.Left - rect.Width / 2f, 0, rect.Left - rect.Width / 2f, rect.Height / 4f); twoShapes.AddLine( rect.Left - rect.Width / 2f, rect.Height / 4f, rect.Left - rect.Width, rect.Height / 4f); twoShapes.AddLine( rect.Left - rect.Width, rect.Height / 4f, rect.Left - rect.Width, rect.Height * 3 / 4f); twoShapes.AddLine( rect.Left - rect.Width, rect.Height * 3 / 4f, rect.Left - rect.Width / 2f, rect.Height * 3 / 4f); twoShapes.AddLine( rect.Left - rect.Width / 2f, rect.Height * 3 / 4f, rect.Left - rect.Width / 2f, rect.Height); twoShapes.Close(); g.DrawPath(redPen, twoShapes, 10, 170); g.FillPath(YellowBrush, twoShapes, 10, 170);

A path is created, and elements are added to it. Because of the Close() statement half-way through, the path is composed of two shapes, and the result of painting the path is shown below.

Usage and limitations

Paths combine a series of lines, curves and other primitives into a single unit that can be drawn or filled with any pen or brush. Because no colour information is stored in the path, it can not be used to group a set of graphics together into a reusable construct. Instead, see Images and stencils for a way to achieve this. The main use of paths is defining complex clipping areas - the shape created by a path can be used to 'cut out' an area of the page so that the graphics operations performed underneath it show through. See Clipping for more details.

Navigation options:

< Colours, pens and brushes | Text, fonts and text styles >