Locked Draw triangle with GDI+

  • Wednesday, April 04, 2007 8:56 PM
     
     

    Hi,

     

    How can you draw a traingle with GDI+. I know the basics of GDI+, but I can't figure it out by myself. I think you need to set up an array of points and then connect then into a path? Can somebody supply me with a code sample and it would also be usefull to see how to fill it. using FillPath???

All Replies

  • Thursday, April 05, 2007 3:58 AM
    Moderator
     
     Answered

    Hi,

    Regarding triangle as a kind of Polygon, you can draw it with DrawPolygon method and fill it with FillPolygon method.

    Sample code:

            private void Form1_Paint(object sender, PaintEventArgs e)
            {
                Point[] points ={ new Point(10, 10), new Point(100, 10), new Point(50, 100) };
                e.Graphics.DrawPolygon(new Pen(Color.Blue), points);
                Point[] points2 ={ new Point(100, 100), new Point(200, 100), new Point(150, 10) };
                e.Graphics.FillPolygon(new SolidBrush(Color.Red), points2);
            }

    Run it, you'll see the effect.

    Thanks

  • Friday, August 17, 2012 8:15 PM
     
     

    You might want to use anti-aliasing while drawing triangles.  Otherwise the points sometimes look like they're off by one.  If you do use anti-aliasing you'll want to set the smoothing mode back to what it was before otherwise it can change the appearance of other things.

    Also, there is a collection of Pens and Brushes if you are using simple colors (Color.Red -> Pens.Red -> Brushes.Red).

    SmoothingMode smoothingModeTmp = e.Graphics.SmoothingMode;
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.FillPolygon(Brushes.Red, points);
    e.Graphics.SmoothingMode = smoothingModeTmp;