In this article

Cropping and Scaling Images in GDI+

In this article

You can use the DrawImage method of the Graphics class to draw and position vector images and raster images. DrawImage is an overloaded method, so there are several ways you can supply it with arguments.

DrawImage Variations

One variation of the DrawImage method receives a Bitmap and a Rectangle. The rectangle specifies the destination for the drawing operation; that is, it specifies the rectangle in which to draw the image. If the size of the destination rectangle is different from the size of the original image, the image is scaled to fit the destination rectangle. The following code example shows how to draw the same image three times: once with no scaling, once with an expansion, and once with a compression:

Some variations of the DrawImage method have a source-rectangle parameter as well as a destination-rectangle parameter. The source-rectangle parameter specifies the portion of the original image to draw. The destination rectangle specifies the rectangle in which to draw that portion of the image. If the size of the destination rectangle is different from the size of the source rectangle, the picture is scaled to fit the destination rectangle.

The following code example shows how to construct a Bitmap from the file Runner.jpg. The entire image is drawn with no scaling at (0, 0). Then a small portion of the image is drawn twice: once with a compression and once with an expansion.