Export (0) Print
Expand All

Rendering Images with GDI+

Visual Studio .NET 2003

You can use GDI+ to render images that exist as files in your applications. You do this by creating a new object of an Image class (such as Bitmap), creating a Graphics object that refers to the drawing surface you want to use, and calling the DrawImage method of the Graphics object. The image will be painted onto the drawing surface represented by the graphics class. You can use the Image Editor to create and edit image files at design time, and render them with GDI+ at run time. For more information, see Image Editor.

To render an image with GDI+

  1. Create an object representing the image you want to display. This object must be a member of a class that inherits from Image, such as Bitmap or MetaFile. An example is shown:
    ' Visual Basic
    ' Uses the System.Environment.GetFolderPath to get the path to the 
    ' current user's MyPictures folder.
    Dim myBitmap as New Bitmap _
       (System.Environment.GetFolderPath _
          (System.Environment.SpecialFolder.MyPictures))
    
    // C#
    // Uses the System.Environment.GetFolderPath to get the path to the 
    // current user's MyPictures folder.
    Bitmap myBitmap = new Bitmap
       (System.Environment.GetFolderPath
          (System.Environment.SpecialFolder.MyPictures));
    
    // C++
    // Uses the System.Environment.GetFolderPath to get the path to the 
    // current user's MyPictures folder.
    Bitmap* myBitmap = new Bitmap
       (System::Environment::GetFolderPath
          (System::Environment::SpecialFolder::MyPictures));
    
  2. Create a Graphics object that represents the drawing surface you want to use. For more information, see Creating Graphical Images with GDI+.
    ' Visual Basic
    ' Creates a Graphics object that represents the drawing surface of 
    ' Button1.
    Dim g as Graphics = Button1.CreateGraphics
    
    // C#
    // Creates a Graphics object that represents the drawing surface of 
    // Button1.
    Graphics g = Button1.CreateGraphics();
    
    // C++
    // Creates a Graphics object that represents the drawing surface of 
    // Button1.
    Graphics* g = button1->CreateGraphics();
    
  3. Call the Graphics.DrawImage Method of your graphics object to render the image. You must specify both the image to be drawn, and the coordinates where it is to be drawn.
    ' Visual Basic
    g.DrawImage(myBitmap, 1, 1)
    
    // C#
    g.DrawImage(myBitmap, 1, 1);
    
    // C#
    g->DrawImage(myBitmap, 1, 1);
    

See Also

GDI+ Example Topics | Creating Graphical Images with GDI+ | Pens, Brushes, and Colors | Drawing Text with GDI+ | GDI+ Graphics | Drawing Lines and Shapes with GDI+ | Image Editor

Show:
© 2014 Microsoft