GDI+ Shapes: Polygons


A polygon is a series of connected lines with the whole shape being closed. In other words, a polygon is defined a group of lines so that, except for the first line of the group, the starting point of each line is the same as the end point of the previous line and the end point of the last line is connected to the start point of the first line.

To draw a polygon, you can use the Graphics.Polygon() method. It is overloaded with two versions as follows:

public void DrawPolygon(Penpen, Point[] points);
public void DrawPolygon(Penpen, PointF[] points);

To use this method, you can first declare a Point or PointF array and pass it as the second argument to the method. Here is an example:

private void Exercise_Paint(object sender, PaintEventArgs e)
    Point[] Pt = { new Point(20, 50),
  		   new Point(180, 50),
		   new Point(180, 20),
                   new Point(230, 70),
		   new Point(180, 120),
		   new Point(180, 90),
       	           new Point(20,  90) };

    Pen penCurrent = new Pen(Color.Red);
    e.Graphics.DrawPolygon(penCurrent, Pt);

This would produce:


Home Copyright © 2010-2021, FunctionX