RenderTargetBitmap and DPI
Asked Answered
A

1

27

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?

Aloud answered 20/4, 2011 at 4:29 Comment(0)
T
3

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 (e.g. 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.

Transmutation answered 20/4, 2011 at 5:33 Comment(2)
I am not sure I understand you. Of course I know what DPI the system uses. Also, I think it is quite normal to not assume that the actual system dpi is 96. My question is both when it is okay to ignore the system dpi and when/why you would want to adjust the size of the image based on the dpi. as mentioned sometimes people seem to adjust the image size based on the dpi and other times not.Aloud
I think you make a false assumption: the dpi of an image has nothing to do with the current system dpi. The dpi of an image specifies how the data in the image should be used/understood.Transmutation

© 2022 - 2024 — McMap. All rights reserved.