Drawing, Positioning, and Cloning Images

You can use the Bitmap class to load and display raster images, and you can use the Metafile class to load and display vector images. The Bitmap and Metafile classes inherit from the Image class. To display a vector image, you need a Graphics object and a Metafile object. To display a raster image, you need a Graphics object and a Bitmap object. The Graphics object provides the DrawImage method, which receives the Metafile or Bitmap object as an argument.

The following example constructs a Bitmap object from the file Climber.jpg and displays the bitmap. The destination point for the upper-left corner of the image, (10, 10), is specified in the second and third parameters.

Dim myBitmap As New Bitmap("Climber.jpg")
myGraphics.DrawImage(myBitmap, 10, 10)
[C#]
Bitmap myBitmap = new Bitmap("Climber.jpg");
myGraphics.DrawImage(myBitmap, 10, 10);

The following illustration shows the image.

sb2z5bb0.aboutgdip03_art04(en-us,VS.71).gif

You can construct Bitmap objects from a variety of graphics file formats: BMP, GIF, JPEG, EXIF, PNG, TIFF, and ICON.

The following example constructs Bitmap objects from a variety of file types and then displays the bitmaps:

Dim myBMP As New Bitmap("SpaceCadet.bmp")
Dim myGIF As New Bitmap("Soda.gif")
Dim myJPEG As New Bitmap("Mango.jpg")
Dim myPNG As New Bitmap("Flowers.png")
Dim myTIFF As New Bitmap("MS.tif")

myGraphics.DrawImage(myBMP, 10, 10)
myGraphics.DrawImage(myGIF, 220, 10)
myGraphics.DrawImage(myJPEG, 280, 10)
myGraphics.DrawImage(myPNG, 150, 200)
myGraphics.DrawImage(myTIFF, 300, 200)
[C#]
Bitmap myBMP = new Bitmap("SpaceCadet.bmp");
Bitmap myGIF = new Bitmap("Soda.gif");
Bitmap myJPEG = new Bitmap("Mango.jpg");
Bitmap myPNG = new Bitmap("Flowers.png");
Bitmap myTIFF = new Bitmap("MS.tif");

myGraphics.DrawImage(myBMP, 10, 10);
myGraphics.DrawImage(myGIF, 220, 10);
myGraphics.DrawImage(myJPEG, 280, 10);
myGraphics.DrawImage(myPNG, 150, 200);
myGraphics.DrawImage(myTIFF, 300, 200);

The Bitmap class provides a Clone method that you can use to make a copy of an existing Bitmap object. The Clone method has a source rectangle parameter that you can use to specify the portion of the original bitmap that you want to copy. The following example creates a Bitmap object by cloning the top half of an existing Bitmap object. Then both images are drawn.

Dim originalBitmap As New Bitmap("Spiral.png")
Dim sourceRectangle As New Rectangle(0, 0, originalBitmap.Width, _
   originalBitmap.Height / 2)

Dim secondBitmap As Bitmap = originalBitmap.Clone(sourceRectangle, _
   PixelFormat.DontCare)

myGraphics.DrawImage(originalBitmap, 10, 10)
myGraphics.DrawImage(secondBitmap, 150, 10)
[C#]
Bitmap originalBitmap = new Bitmap("Spiral.png");
Rectangle sourceRectangle = new Rectangle(0,0, originalBitmap.Width,
   originalBitmap.Height/2);

Bitmap secondBitmap = originalBitmap.Clone(sourceRectangle,
   PixelFormat.DontCare);

myGraphics.DrawImage(originalBitmap, 10, 10);
myGraphics.DrawImage(secondBitmap, 150, 10);

The following illustration shows the two images.

sb2z5bb0.aboutgdip03_art05(en-us,VS.71).gif