Export (0) Print
Expand All

Creating Graphical Images with GDI+

Visual Studio .NET 2003

Before you can draw lines and shapes, render text, or display and manipulate images with GDI+, you need to create a Graphics object. The Graphics object represents a GDI+ drawing surface, and is the object that is used to create graphical images.

There are two steps in working with graphics:

  1. Creating a Graphics object.
  2. Using the Graphics object to draw lines and shapes, render text, or display and manipulate images.

Creating a Graphics Object

A graphics object can be created in a variety of ways:

  • By receiving a reference to a graphics object as part of the PaintEventArgs in the Paint event of a form or control. This is usually how you obtain a reference to a graphics object when creating painting code for a control.
  • By calling the CreateGraphics method of a control or form to obtain a reference to a Graphics object that represents the drawing surface of that control or form. You would use this method if you wanted to draw on a form or control that already existed.
  • By creating a Graphics object from any object that inherits from Image. This approach is useful when you want to alter an already existing image.

PaintEventArgs in the Paint Event Handler

When programming the Paint event handler for controls, a graphics object is provided as one of the PaintEventArgs.

To obtain a reference to a Graphics object from the PaintEventArgs in the Paint event

  1. Declare the Graphics object.
  2. Assign the variable to refer to the Graphics object passed as part of the PaintEventArgs.
  3. Insert code to paint the form or control.

The following example shows how to reference a Graphics object from the PaintEventArgs in the Paint event:

' Visual Basic
Private Sub Form1_Paint(sender As Object, pe As PaintEventArgs) Handles _
   MyBase.Paint
   ' Declares the Graphics object and sets it to the Graphics object
   ' supplied in the PaintEventArgs.
   Dim g As Graphics = pe.Graphics
   ' Insert code to paint the form here.
End Sub

// C#
private void Form1_Paint(object sender, 
   System.Windows.Forms.PaintEventArgs pe) 
{
   // Declares the Graphics object and sets it to the Graphics object
   // supplied in the PaintEventArgs.
   Graphics g = pe.Graphics;
   // Insert code to paint the form here.
}

// C++
private:
   System::Void Form1_Paint(System::Object *  sender,
      System::Windows::Forms::PaintEventArgs *  pe)
   {
      // Declares the Graphics object and sets it to the Graphics object
      // supplied in the PaintEventArgs.
      Graphics * g = pe->Graphics;
      // Insert code to paint the form here.
   }

CreateGraphics Method

You can also use the CreateGraphics method of a control or form to obtain a reference to a Graphics object that represents the drawing surface of that control or form.

To create a Graphics object with the CreateGraphics method

  • Call the CreateGraphics method of the form or control upon which you want to render graphics.
    ' Visual Basic
    Dim g as Graphics
    ' Sets g to a Graphics object representing the drawing surface of the
    ' control or form g is a member of.
    g = Me.CreateGraphics
    
    // C#
    Graphics g;
    // Sets g to a graphics object representing the drawing surface of the
    // control or form g is a member of.
    g = this.CreateGraphics();
    
    // C++
    Graphics * g;
    // Sets g to a graphics object representing the drawing surface of the
    // control or form g is a member of.
    g = this->CreateGraphics();
    

Create from an Image object

Additionally, you can create a graphics object from any object that derives from the Image class.

To create a Graphics object from an Image

  • Call the Graphics.FromImage method, supplying the name of the Image variable from which you want to create a Graphics object.

The following example shows how to use a Bitmap object:

' Visual Basic
Dim myBitmap as New Bitmap("C:\Documents and Settings\Joe\Pics\myPic.bmp")
Dim g as Graphics = Graphics.FromImage(myBitmap)

// C#
Bitmap myBitmap = new Bitmap(@"C:\Documents and 
   Settings\Joe\Pics\myPic.bmp");
Graphics g = Graphics.FromImage(myBitmap);

// C++
Bitmap * myBitmap = new
   Bitmap(S"D:\\Documents and Settings\\Joe\\Pics\\myPic.bmp");
Graphics * g = Graphics::FromImage(myBitmap);
Note   You can only create Graphics objects from non-indexed .bmp files, such as 16-bit, 24-bit, and 32-bit .bmp files. Each pixel of non-indexed .bmp files holds a color, in contrast to pixels of indexed .bmp files, which hold an index to a color table.

Drawing and Manipulating Shapes and Images

After it is created, a Graphics object may be used to draw lines and shapes, render text, or display and manipulate images. The principal objects that are used with the Graphics object are:

  • Pen Class - Used for drawing lines, outlining shapes, or rendering other geometric representations.
  • Brush Class - Used for filling areas of graphics, such as filled shapes, images, or text.
  • Font Class - Provides a description of what shapes to use when rendering text.
  • Color Structure - Represents the different colors to display.

For more information, see the following topics:

See Also

GDI+ Example Topics | GDI+ Graphics | Pens, Brushes, and Colors | Rendering Images with GDI+

Show:
© 2015 Microsoft