Drawing Polygons in C# .NET

Saturday 22 September 2012

                    You can also draw irregular shapes, like Polygons. To do that, you set up a series of points, and hand them to the DrawPolygon method.

                    The first thing to do is to set up an array of points:

Point[] polygonPoints = new Point[5];
              
                    This sets up a new point array called polygonPoints. We're specified 5 sets of points. The points are X, Y coordinates. X is how far to the left you want the point and Y is how far down. You set them up like this:

polygonPoints[0] = new Point(113, 283);
polygonPoints[1] = new Point(70, 156);
polygonPoints[2] = new Point(180, 70);
polygonPoints[3] = new Point(290, 156);
polygonPoints[4] = new Point(250, 283);

                  So each slot in the array is filled with a new point. In between the round brackets of Point, you add your X, Y coordinates.
Once you have your points array, you can hand it to the DrawPolygon method:

surface.DrawPolygon( pen1, polygonPoints );

                After specifying what you want to draw on (surface, for us), you type a dot followed by DrawPolygon. In between the round brackets of DrawPolygon you first need a pen. Then you need your points array. Note that the brackets of the array are not needed.
If you want to fill your polygon, you can use the FillPolygon method:

surface.FillPolygon( brushOne, polygonPoints );

               FillPolygon needs a brush, and the points array again. When the above code is run, it will produce the following polygon:



3 comments:

  1. Thanks Vishal for sharing your knowledge. I have created my graphing app in VS using some of the elements you explain in this post: http://ortizol.blogspot.com/2014/06/nivel-1-en-csharp-el-triangulo.html

    See you later.

  1. Unknown said...:

    Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

  1. Mounika said...:

    Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    python Training in Pune
    python Training in Chennai
    python Training in Bangalore

Post a Comment