简体   繁体   中英

RenderTargetBitmap and DPI

I don't understand why the DPI parameters on the RenderTargetBitmap are used the way they seem to be used. Quite often I see code that deals with non-standard DPI settings by adjusting the image size based on the DPI like this:

new RenderTargetBitmap ((int) (width/96d)*dpi, (int) (height/96d)*dpi, dpi, dpi, PixelFormats.Pbgra32);

Examples:

http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx (via web.archive.org )

ContentControl + RenderTargetBitmap + empty image

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/984da366-33d3-4fd3-b4bd-4782971785f8/

Other times the dpi seems to be hard coded to either 96 or different values like so:

new RenderTargetBitmap (width, height, 96, 96, PixelFormats.Pbgra32);

Examples:

http://www.i-programmer.info/programming/wpf-workings/538-rendertargetbitmap-visual-vector-to-bitmap.html

http://www.ericsink.com/wpf3d/3_Bitmap.html

http://dedjo.blogspot.com/2008/05/performance-appliance-of.html

How to render a checked CheckBox (Aero theme) to a RenderTargetBitmap?

or

new RenderTargetBitmap (width, height, 120, 96, PixelFormats.Pbgra32);

Examples:

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx

When would you do the one and one the other?

Am I right to say that you should always adjust the size of the bitmap (as in the first example) if you later want to display the image with the same size as the control inside your app?

And that you probably should use a fixed dpi at 96 when saving it to file, or if you are using a different dpi not adjust the width and height?

Say I want to save the image of a control to a file with a fixed size. Would I then still set the dpi or just use the default 96?

The default of 96 is just the most general/used dpi of desktop displays. But that will/might change.

If you know what dpi the bitmap must have during compilation of your program you could set it at the desired value (eg 96) The be honest, I'd use a constant to do that but it boils down to the same thing at runtime.

If you do not know at what resolution the image will be needed you shouldn't hard-code the dpi.

By the way: make sure you always set the dpi of an image correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM