다음을 통해 공유


Improving Performance by Avoiding Automatic Scaling

If you pass only the upper-left corner of an image to the DrawImage method, GDI+ might scale the image, which would decrease performance.

The following call to the DrawImage method specifies an upper-left corner of (50, 30) but does not specify a destination rectangle:

e.Graphics.DrawImage(image, 50, 30) ' upper-left corner at (50, 30)
[C#]
e.Graphics.DrawImage(image, 50, 30);  // upper-left corner at (50, 30)

Although this is the easiest version of the DrawImage 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 Image object, then DrawImage will scale the image. For example, suppose an Image object has a width of 216 pixels and a stored horizontal resolution value of 72 dots per inch. Because 216/72 is 3, DrawImage will scale the image so that it has a width of 3 inches at a resolution of 96 dots per inch. That is, DrawImage 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+ 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.

If you want to prevent such scaling, pass the width and height of a destination rectangle to the DrawImage method. 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.

Dim image = New Bitmap("Texture.jpg")

e.Graphics.DrawImage(image, 10, 10)
e.Graphics.DrawImage(image, 120, 10, image.Width, image.Height)
[C#]
Image image = new Bitmap("Texture.jpg");

e.Graphics.DrawImage(image, 10, 10);
e.Graphics.DrawImage(image, 120, 10, image.Width, image.Height);

The following illustration shows the image rendered twice.

1bttkazd.csscaledtexture1(en-us,VS.71).gif