Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

winforms - .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

See also

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...