Export (0) Print
Expand All
5 out of 11 rated this helpful - Rate this topic

Drawing Lines and Shapes with GDI+

Visual Studio .NET 2003

The Graphics object provides methods for drawing a variety of lines and shapes. Simple or complex shapes can be rendered in solid or transparent colors, or using user-defined gradient or image textures. Lines, open curves, and outline shapes are created using a Pen object. To fill in an area, such as a rectangle or a closed curve, a Brush object is required. For more information about how to create and use pens and brushes, see Pens, Brushes, and Colors.

To draw a line or outlined shape

  1. Obtain a reference to the graphics object you will use for drawing. For more information, see Creating Graphical Images with GDI+.
    ' Visual Basic
    ' Obtains a reference to the graphics object for Button1.
    Dim g as Graphics = Button1.CreateGraphics
    
    // C#
    Graphics g = Button1.CreateGraphics();
    
    // C++
    Graphics* g = button1->CreateGraphics();
    
  2. Create an instance of the Pen Class you want to use to draw the line and set any appropriate properties.
    ' Visual Basic
    Dim myPen as new Pen(Color.Red)
    myPen.Width = 5
    
    // C#
    Pen myPen = new Pen(Color.Red);
    myPen.Width = 5;
    
    // C++
    Pen* myPen = new Pen(Color::Red);
    myPen->Width = 5;
    
  3. Call the method appropriate to the shape you want to draw, supplying any required parameters. The following table lists a few of the most common methods. For a complete list, see Graphics Methods.
    MethodShape
    Graphics.DrawLine MethodLine; requires coordinates to indicate starting and ending points.
    Graphics.DrawPolygon MethodComplex shapes; may require an array of coordinates.
    Graphics.DrawRectangle MethodRectangle; requires one or more objects (such as a Rectangle object) as parameters.
    ' Visual Basic
    g.DrawLine(myPen, 1, 1, 45, 65)
    g.DrawBezier(myPen, 15, 15, 30, 30, 45, 30, 87, 20)
    g.DrawEllipse(myPen, New Rectangle(33, 45, 40, 50))
    g.DrawPolygon(myPen, New PointF() {New PointF(1, 1), _
       New PointF (20, 10), New PointF(5, 4), New PointF(100, 2), _
       New PointF(200, 50), New PointF(39, 45)})
    
    // C#
    g.DrawLine(myPen, 1, 1, 45, 65);
    g.DrawBezier(myPen, 15, 15, 30, 30, 45, 30, 87, 20);
    g.DrawEllipse(myPen, new Rectangle(33, 45, 40, 50));
    g.DrawPolygon(myPen, new PointF[] {new PointF(1, 1), 
       new PointF (20, 10), new PointF(5, 4), new PointF(100, 2), 
       new PointF(200, 50), new PointF(39, 45)});
    
    // C++
    g->DrawLine(myPen, 1, 1, 45, 65);
    g->DrawBezier(myPen, 15, 15, 30, 30, 45, 30, 87, 20);
    g->DrawEllipse(myPen, Rectangle(33, 45, 40, 50));
    PointF myPointFArray[] = {PointF(1, 1), 
       PointF(20, 10), PointF(5, 4), PointF(100, 2),
       PointF(200, 50), PointF(39, 45)};
    g->DrawPolygon(myPen, myPointFArray);
    

To draw a filled shape

  1. Obtain a reference to the graphics object you will use for drawing. For more information, see Creating Graphical Images with GDI+.
    ' Visual Basic
    ' Obtains a reference to the graphics object for Button1.
    Dim g as Graphics = Button1.CreateGraphics
    
    // C#
    Graphics g = Button1.CreateGraphics;
    
    // C++
    Graphics* g = button1->CreateGraphics();
    
  2. Create an instance of the Brush you want to use to paint the shape.
    ' Visual Basic
    Dim myBrush as New SolidBrush(Color.Red)
    
    // C#
    SolidBrush myBrush = new SolidBrush(Color.Red);
    
    // C++
    SolidBrush* myBrush = new SolidBrush(Color::Red);
    
  3. Call the method appropriate to the shape that you want to paint, supplying any appropriate parameters. For some methods, such as FillPolygon, you will have to supply an array of points that describe the outline of the shape you want to paint. Other methods, such as FillRectangle or FillPath, require an object that describes the area to be filled. For further information on what parameters are required, consult the Help topic for the method you are using. Examples are shown below:
    ' Visual Basic
    g.FillPolygon(myBrush, New PointF() {New PointF(20, 20), _
       New PointF(50, 100), New PointF(60, 10), New PointF(200, 4), _
       New PointF(0, 0), New PointF(20, 20)})
    g.FillRectangle(myBrush, New RectangleF(50, 50, 100, 100))
    g.FillPie(myBrush, New Rectangle(110, 110, 300, 300), 0, 90)
    
    // C#
    g.FillPolygon(myBrush, new PointF[] {new PointF(20, 20), 
       new PointF(50, 100), new PointF(60, 10), new PointF(200, 4), 
       new PointF(0, 0), new PointF(20, 20)});
    g.FillRectangle(myBrush, new RectangleF(50, 50, 100, 100));
    g.FillPie(myBrush, new Rectangle(110, 110, 300, 300), 0, 90);
    
    // C++
    PointF myPointFArray[] = {PointF(20, 20), 
       PointF(50, 100), PointF(60, 10), PointF(200, 4), 
       PointF(0, 0), PointF(20, 20)};
    g->FillPolygon(myBrush, myPointFArray);
    g->FillRectangle(myBrush, RectangleF(50, 50, 100, 100));
    g->FillPie(myBrush, Rectangle(110, 110, 300, 300), 0, 90);
    

See Also

GDI+ Example Topics | Creating Graphical Images with GDI+ | Pens, Brushes, and Colors | Drawing Text With GDI+ | Rendering Images With GDI+ | GDI+ Graphics

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.