Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: What does Graphics.DrawImageUnscaled do?

It is not well known, that if you draw an image, e.g.:

graphics.DrawImage(image, top, left);

the image will be scaled. This is because DrawImage looks at the dpi setting of the image (e.g. 72dpi from photoshop), and scales the image to match the destination display (typically 96dpi).

If you want to draw an image without any scaling, you must explicitly give DrawImage the size of the image:

graphics.DrawImage(img, top, left, img.Width, img.Height);

i knew this from years of programming in GDI+. The same fact exists in the .NET System.Drawing wrappers around GDI+ - if you want to draw an image unscaled you must force it to scale to the original size.

Which is why i was impressed to find a DrawImageUnscaled method in .NET:

graphics.DrawImageUnscaled(img, top, left);

Except that the image is still scaled; making it identical to:

graphics.DrawImage(img, top, left);

and if you want to draw an image unscaled you must continue to call:

graphics.DrawImage(img, top, left, img.Width, img.Height);

Which brings me to my question: what does DrawImageUnscaled if not to draw an image unscaled?

From MSDN

  • Graphics.DrawImageUnscaled Method (Image, Int32, Int32)

    Draws the specified image using its original physical size at the location specified by a coordinate pair.

  • Graphics.DrawImage Method (Image, Int32, Int32)

    Draws the specified image, using its original physical size, at the location specified by a coordinate pair.

  • Graphics.DrawImage Method (Image, Int32, Int32, Int32, Int32)

    Draws the specified Image at the specified location and with the specified size.

See also

  • MSDN: How to: Improve Performance by Avoiding Automatic Scaling
like image 886
Ian Boyd Avatar asked Nov 09 '11 21:11

Ian Boyd


1 Answers

Well Graphics.DrawImageUnscaled Method (Image, Int32, Int32) doesn't do anything different than DrawImage (Image, Int32, Int32)

e.g.

public void DrawImageUnscaled(Image image, int x, int y)
{
    this.DrawImage(image, x, y);
}

However the methods that take in a Height, Width or a rectangle are different. Those methods either ignore the height and width or with the rectangle only use the top and left.

My guess is that DrawImageUnscaled Method (Image, Int32, Int32) exists for parity reasons and doesn't have to do with scaling due to dpi difference in the source and target device.

like image 157
Conrad Frix Avatar answered Nov 20 '22 20:11

Conrad Frix