Search code examples
c#graphicsdrawing2d

C# GDI Drawing2D help


What GDI methods can I use to draw the blue shape shown in the image below? The center must be transparent.

alt text


Solution

  • There are a number of ways but you'll probably want to use the following:

    FillRectangle
    FillPolygon
    DrawLine
    

    since it looks like your shape can be reduced to a rectangle and two polygons and then outlined by a few lines.

    Here is a really simple and hard-coded example of what i was thinking:

    Private Sub Control_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) _
    Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
    
        g.FillRectangle(Brushes.Aqua, New Rectangle(10, 10, 10, 90))
        g.FillPolygon(Brushes.Aqua, New Point() { _
            New Point(10, 10), _
            New Point(20, 10), _
            New Point(40, 50), _
            New Point(30, 50)})
        g.FillPolygon(Brushes.Aqua, New Point() { _
            New Point(10, 100), _
            New Point(20, 100), _
            New Point(40, 50), _
            New Point(30, 50)})
        g.DrawLine(Pens.Black, New Point(10, 10), New Point(10, 100))
        g.DrawLine(Pens.Black, New Point(10, 100), New Point(20, 100))
        g.DrawLine(Pens.Black, New Point(20, 100), New Point(40, 50))
        g.DrawLine(Pens.Black, New Point(40, 50), New Point(20, 10))
        g.DrawLine(Pens.Black, New Point(20, 10), New Point(10, 10))
    

    ...