简体   繁体   English

RenderTargetBitmap 和 DPI

[英]RenderTargetBitmap and DPI

I don't understand why the DPI parameters on the RenderTargetBitmap are used the way they seem to be used.我不明白为什么RenderTargetBitmap上的 DPI 参数以它们似乎使用的方式使用。 Quite often I see code that deals with non-standard DPI settings by adjusting the image size based on the DPI like this:我经常看到通过根据 DPI 调整图像大小来处理非标准 DPI 设置的代码,如下所示:

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 ) http://blogs.msdn.com/b/jaimer/archive/2009/07/03/rendertargetbitmap-tips.aspx (通过web.archive.org

ContentControl + RenderTargetBitmap + empty image ContentControl + RenderTargetBitmap + 空图像

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/984da366-33d3-4fd3-b4bd-4782971785f8/ 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:其他时候 dpi 似乎被硬编码为 96 或不同的值,如下所示:

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.i-programmer.info/programming/wpf-workings/538-rendertargetbitmap-visual-vector-to-bitmap.html

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

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

How to render a checked CheckBox (Aero theme) to a RenderTargetBitmap? 如何将选中的 CheckBox(Aero 主题)渲染到 RenderTargetBitmap?

or或者

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

Examples:例子:

http://msdn.microsoft.com/en-us/library/system.windows.media.imaging.rendertargetbitmap.aspx 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?并且您可能应该在将其保存到文件时使用 96 的固定 dpi,或者如果您使用不同的 dpi 不调整宽度和高度?

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?然后我仍然设置 dpi 还是只使用默认的 96?

The default of 96 is just the most general/used dpi of desktop displays.默认值 96 只是桌面显示器最通用/最常用的 dpi。 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.如果您知道在程序编译期间位图必须具有什么 dpi,您可以将其设置为所需的值(例如 96)。老实说,我会使用一个常量来做到这一点,但在运行时它归结为同样的事情。

If you do not know at what resolution the image will be needed you shouldn't hard-code the dpi.如果您不知道需要什么分辨率的图像,则不应对 dpi 进行硬编码。

By the way: make sure you always set the dpi of an image correctly.顺便说一句:确保您始终正确设置图像的 dpi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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