任何人都可以建议何时使用 SnapsToDevicePixels
的指南吗?在 WPF 4.0 中?
是否应该仅在出现问题时偶尔使用它,在整个应用程序中广泛使用,仅在某些控件或什么上使用?
最佳答案
斯宾塞和马丁对于何时对齐像素给出了很好的答案。
至于如何:我还想指出,在 WPF 4.0 中应该尝试使用属性 UseLayoutRounding
而不是 SnapsToDevicePixels
.
UseLayoutRounding
使您所做的事情与 Silverlight 兼容( SnapsToDevicePixels
在 Silverlight 中不可用)...并且 Microsoft 还鼓励使用 UseLayoutRounding
超过SnapsToDevicePixels
在其 documentation .
两者有什么区别?嗯,一个很大的区别是 UseLayoutRounding
发生在布局阶段,而 SnapsToDevicePixels
发生在渲染阶段。这让我推测 UseLayoutRounding
可能是一种更高效的方法(不过我还没有证实这一点)。
尽管如此,仍然有理由使用 SnapsToDevicePixels
。事实上,MSDN 文档指出了其中之一。我将添加另一个:它仅适用于 SnapsToDevicePixels
您可以使用指南进行精确控制。
以下是有关此问题的一些资源(即像素捕捉以及图像、文本和视觉效果的清晰度):
- The MSDN Documentation for the UIElement.SnapsToDevicePixels Property.
- The MSDN Documentation for the FrameworkElement.UseLayoutRounding Property.
- Here is the old, classic post by Dwayne Need on blurry images.
- Sometimes BitmapScalingMode helps with image clarity.
- A great blog post that shows you how to actually use SnapsToDevicePixels. It can be tricky.
- A blog post by the WPF Text team about all the different improvements they added to WPF 4.0 in order to help text clarity.
- Another blog post by the WPF Text team on layout rounding.
嘿。我知道我的答案比你所要求的要多一点......但是这个概念(即分辨率独立性及其带来的问题以及如何克服它们)通常可以成为 frustration 的要点。使用 WPF 时。至少,我想向您指出新的 WPF 4.0 属性 UseLayoutRounding
.
更新
我只需要补充一下,因为我一遍又一遍地看到这个......有时SnapsToDevicePixels
工作时 UseLayoutRounding
没有。我希望我能弄清楚为什么会出现这种情况,但一定要先尝试 UseLayoutRounding,如果这不起作用,请毫不犹豫地尝试 SnapsToDevicePixels
.
那条线太锋利了,会割伤你!
关于.net - 我什么时候应该在 WPF 4.0 中使用 SnapsToDevicePixels?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2399731/