Share via


Polygons in GDI+

A polygon is a closed shape with three or more straight sides. For example, a triangle is a polygon with three sides, a rectangle is a polygon with four sides, and a pentagon is a polygon with five sides. The following illustration shows several polygons.

Polygons

Drawing a Polygon

To draw a polygon, you need a Graphics object, a Pen object, and an array of Point (or PointF) objects. The Graphics object provides the DrawPolygon method. The Pen object stores attributes, such as width and color, of the line used to render the polygon, and the array of Point objects stores the points to be connected by straight lines. The Pen object and the array of Point objects are passed as arguments to the DrawPolygon method. The following example draws a three-sided polygon. Note that there are only three points in myPointArray: (0, 0), (50, 30), and (30, 60). The DrawPolygon method automatically closes the polygon by drawing a line from (30, 60) back to the starting point (0, 0).

Dim myPointArray As Point() = _
   {New Point(0, 0), New Point(50, 30), New Point(30, 60)}
myGraphics.DrawPolygon(myPen, myPointArray)
     Point[] myPointArray = 
{ new Point(0, 0), new Point(50, 30), new Point(30, 60) };
     myGraphics.DrawPolygon(myPen, myPointArray);

The following illustration shows the polygon.

Polygon

See Also

Tasks

How to: Create a Pen

Reference

System.Drawing.Graphics
System.Drawing.Pen

Other Resources

Lines, Curves, and Shapes