Files
docs-desktop/dotnet-desktop-guide/framework/winforms/advanced/how-to-improve-performance-by-avoiding-automatic-scaling.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.9 KiB

title, ms.date, dev_langs, helpviewer_keywords, ms.assetid
title ms.date dev_langs helpviewer_keywords ms.assetid
How to: Improve Performance by Avoiding Automatic Scaling 03/30/2017
csharp
vb
automatic scaling
images [Windows Forms], improving performance
images [Windows Forms], using without automatic scaling
performance [Windows Forms], improving image
5fe2c95d-8653-4d55-bf0d-e5afa28f223b

How to: Improve Performance by Avoiding Automatic Scaling

GDI+ may automatically scale an image as you draw it, which would decrease performance. Alternatively, you can control the scaling of the image by passing the dimensions of the destination rectangle to the xref:System.Drawing.Graphics.DrawImage%2A method.

For example, the following call to the xref:System.Drawing.Graphics.DrawImage%2A method specifies an upper-left corner of (50, 30) but does not specify a destination rectangle.

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

Although this is the easiest version of the xref:System.Drawing.Graphics.DrawImage%2A method in terms of the number of required arguments, it is not necessarily the most efficient. If the resolution used by GDI+ (usually 96 dots per inch) is different from the resolution stored in the xref:System.Drawing.Image object, then the xref:System.Drawing.Graphics.DrawImage%2A method will scale the image. For example, suppose an xref:System.Drawing.Image object has a width of 216 pixels and a stored horizontal resolution value of 72 dots per inch. Because 216/72 is 3, xref:System.Drawing.Graphics.DrawImage%2A will scale the image so that it has a width of 3 inches at a resolution of 96 dots per inch. That is, xref:System.Drawing.Graphics.DrawImage%2A will display an image that has a width of 96x3 = 288 pixels.

Even if your screen resolution is different from 96 dots per inch, GDI+ will probably scale the image as if the screen resolution were 96 dots per inch. That is because a GDI+ xref:System.Drawing.Graphics object is associated with a device context, and when GDI+ queries the device context for the screen resolution, the result is usually 96, regardless of the actual screen resolution. You can avoid automatic scaling by specifying the destination rectangle in the xref:System.Drawing.Graphics.DrawImage%2A method.

Example

The following example draws the same image twice. In the first case, the width and height of the destination rectangle are not specified, and the image is automatically scaled. In the second case, the width and height (measured in pixels) of the destination rectangle are specified to be the same as the width and height of the original image. The following illustration shows the image rendered twice:

Screenshot that shows images with scaled texture.

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

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. Replace Texture.jpg with an image name and path that are valid on your system.

See also