Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/cropping-and-scaling-images-in-gdi.md
T
Andy De George c0ba284473 Initial winforms content migrated (#18)
* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
2020-09-01 16:26:21 -07:00

3.3 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
Cropping and Scaling Images in GDI+ 03/30/2017
csharp
vb
GDI+, scaling images
GDI+, cropping images
images [Windows Forms], cropping
compressing data [Windows Forms], images
images [Windows Forms], expansion
images [Windows Forms], scaling
rectangles [Windows Forms], source
rectangles [Windows Forms], destination
images [Windows Forms], compression
ad5daf26-005f-45bc-a2af-e0e97777a21a

Cropping and Scaling Images in GDI+

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

DrawImage Variations

One variation of the xref:System.Drawing.Graphics.DrawImage%2A method receives a xref:System.Drawing.Bitmap and a xref:System.Drawing.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:

[!code-csharpSystem.Drawing.ImagesBitmapsMetafiles#31] [!code-vbSystem.Drawing.ImagesBitmapsMetafiles#31]

The following illustration shows the three pictures.

Scaling

Some variations of the xref:System.Drawing.Graphics.DrawImage%2A 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 xref:System.Drawing.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.

[!code-csharpSystem.Drawing.ImagesBitmapsMetafiles#32] [!code-vbSystem.Drawing.ImagesBitmapsMetafiles#32]

The following illustration shows the unscaled image, and the compressed and expanded image portions.

Cropping and Scaling

See also