다음을 통해 공유


Drawing, Positioning, and Cloning Images in GDI+

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 an instance of the Graphics class and a Metafile. To display a raster image, you need an instance of the Graphics class and a Bitmap. The instance of the Graphics class provides the DrawImage method, which receives the Metafile or Bitmap as an argument.

File Types and Cloning

The following code example shows how to construct a Bitmap 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)
Bitmap myBitmap = new Bitmap("Climber.jpg");
myGraphics.DrawImage(myBitmap, 10, 10);

The following illustration shows the image.

Image Sample

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

The following code example shows how to construct 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)
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. 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 code example shows how to create a Bitmap by cloning the top half of an existing Bitmap. Then both images are drawn.

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

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

myGraphics.DrawImage(originalBitmap, 10, 10)
myGraphics.DrawImage(secondBitmap, 150, 10)
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.

Cropping

See Also

Tasks

How to: Create Graphics Objects for Drawing

Other Resources

Images, Bitmaps, and Metafiles
Working with Images, Bitmaps, Icons, and Metafiles