Files
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

2.5 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Crop and Scale Images 03/30/2017
csharp
vb
images [Windows Forms], cropping
images [Windows Forms], scaling
053e3360-bca0-4b25-9afa-0e77a6f17b03

How to: Crop and Scale Images

The xref:System.Drawing.Graphics class provides several xref:System.Drawing.Graphics.DrawImage%2A methods, some of which have source and destination rectangle parameters that you can use to crop and scale images.

Example

The following example constructs an xref:System.Drawing.Image object from the disk file Apple.gif. The code draws the entire apple image in its original size. The code then calls the xref:System.Drawing.Graphics.DrawImage%2A method of a xref:System.Drawing.Graphics object to draw a portion of the apple image in a destination rectangle that is larger than the original apple image.

The xref:System.Drawing.Graphics.DrawImage%2A method determines which portion of the apple to draw by looking at the source rectangle, which is specified by the third, fourth, fifth, and sixth arguments. In this case, the apple is cropped to 75 percent of its width and 75 percent of its height.

The xref:System.Drawing.Graphics.DrawImage%2A method determines where to draw the cropped apple and how big to make the cropped apple by looking at the destination rectangle, which is specified by the second argument. In this case, the destination rectangle is 30 percent wider and 30 percent taller than the original image.

The following illustration shows the original apple and the scaled, cropped apple.

Screenshot of an original image and the same image cropped.

[!code-csharpSystem.Drawing.WorkingWithImages#11] [!code-vbSystem.Drawing.WorkingWithImages#11]

Compiling the Code

The preceding example is designed for use with Windows Forms, and it requires xref:System.Windows.Forms.PaintEventArgs e, which is a parameter of the xref:System.Windows.Forms.Control.Paint event handler. Make sure to replace Apple.gif with an image file name and path that are valid on your system.

See also