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.


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).

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

The following illustration shows the polygon.


Lines, Curves, and Shapes
How to: Create a Pen