.net 我应该什么时候在 WPF 4.0 中使用 SnapsToDevicePixels?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2399731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When should I use SnapsToDevicePixels in WPF 4.0?
提问by Mike B
Can anyone suggest a guideline on when to use SnapsToDevicePixelsin WPF 4.0?
任何人都可以提出有关何时SnapsToDevicePixels在 WPF 4.0 中使用的指南吗?
Should it only be used occasionally if there is an issue, liberally throughout an App, only on certain controls or what?
是否应该仅在出现问题时偶尔使用,在整个应用程序中广泛使用,仅在某些控件上使用还是什么?
回答by cplotts
Good answers by Spencer and Martin as to the whento align your pixels.
Spencer 和 Martin 关于何时对齐像素的好答案。
As to the how: I would also point out that one should in WPF 4.0try using the property UseLayoutRoundinginstead of SnapsToDevicePixels.
至于如何:我还要指出,应该在 WPF 4.0 中尝试使用属性UseLayoutRounding而不是SnapsToDevicePixels.
UseLayoutRoundingmakes what you are doing compatible with Silverlight (SnapsToDevicePixelsis not available in Silverlight) ... and Microsoft is also encouraging the use of UseLayoutRoundingover SnapsToDevicePixelsin its documentation.
UseLayoutRounding使您正在做的事情与 Silverlight 兼容(SnapsToDevicePixels在 Silverlight 中不可用)......并且微软也鼓励在其文档中使用UseLayoutRoundingover 。SnapsToDevicePixels
What is the difference between the two? Well, one big difference is that UseLayoutRoundingoccurs during the layout phase while SnapsToDevicePixelsoccurs during the render phase. This makes me speculate that UseLayoutRoundingis probably a more performant way to go (I haven't confirmed this, though).
两者有什么区别?嗯,一个很大的区别是UseLayoutRounding在布局阶段SnapsToDevicePixels发生,而在渲染阶段发生。这让我推测这UseLayoutRounding可能是一种更高效的方式(不过我还没有证实这一点)。
All that being said, there will still be reasons to use SnapsToDevicePixels. In fact, the MSDN documentation points to one. I will add another: it is only with SnapsToDevicePixelsthat you can use guidelines for precise control.
尽管如此,仍然有理由使用SnapsToDevicePixels. 事实上,MSDN 文档指向了一个。我再补充一点:只有SnapsToDevicePixels这样,您才能使用指南进行精确控制。
Here are some resources on this matter (i.e. pixel snapping and clarity with images, text, and visuals):
以下是有关此问题的一些资源(即图像、文本和视觉效果的像素捕捉和清晰度):
- 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.
- UIElement.SnapsToDevicePixels 属性的 MSDN 文档。
- FrameworkElement.UseLayoutRounding 属性的 MSDN 文档。
- 这是 Dwayne Need 关于模糊图像的古老经典帖子。
- 有时 BitmapScalingMode 有助于提高图像清晰度。
- 一篇很棒的博客文章,向您展示了如何实际使用 SnapsToDevicePixels。这可能很棘手。
- WPF Text 团队的一篇博客文章,介绍了他们为提高文本清晰度而向 WPF 4.0 添加的所有不同改进。
- WPF Text 团队关于布局舍入的另一篇博文。
Heh. I know my answer was a little more than what you were asking for ... but this concept (i.e. resolution independence and the resulting problems it brings and how to get over them) can often be a point of frustrationwhen working with WPF. At the very least, I wanted to point you to the new WPF 4.0 property, UseLayoutRounding.
呵呵。我知道我的回答比您要求的要多一些……但是这个概念(即分辨率独立性及其带来的问题以及如何克服它们)在使用 WPF 时通常会令人沮丧。至少,我想向您指出新的 WPF 4.0 属性UseLayoutRounding.
UPDATE
更新
I just have to add since I've seen this over and over ... sometimes SnapsToDevicePixelsworks when UseLayoutRoundingdoesn't. I wish I could put a finger on why this is the case, but definitely try UseLayoutRounding first and if that doesn't work, don't hesitate to try SnapsToDevicePixels.
我只需要添加,因为我已经看到了这个一遍又一遍......有时候SnapsToDevicePixels,当工作UseLayoutRounding没有。我希望我能指出为什么会出现这种情况,但一定要先尝试 UseLayoutRounding,如果这不起作用,请不要犹豫,尝试SnapsToDevicePixels.
That line is so sharp it can cut you!
那条线太锋利了,可以割伤你!
回答by Spencer Ruport
It should be used on controls or areas where the placement of the pixels has meaning. Controls relating to the canvas of a drawing application would be one example. Have you ever seen the map of a fragmented drive? This might be another example.
它应该用于像素放置有意义的控件或区域。与绘图应用程序的画布相关的控件就是一个例子。你见过碎片化驱动器的地图吗?这可能是另一个例子。
One exception I can think of is when you're using divider lines of some kind. Most people expect border lines to be solid. If this setting is off they can look blurred and distracting.
我能想到的一个例外是当您使用某种分隔线时。大多数人希望边界线是实心的。如果此设置关闭,它们可能看起来模糊和分散注意力。
Basically if blurred edges = bad then turn it on
基本上,如果边缘模糊 = 坏,则将其打开
回答by Warpin
One case is if you are displaying an image or video. If you don't snap to device pixels (ie to the video screen's pixels) then some algorithm (interpolation, anti-aliasing) is used to position your image's pixels "in between" your screen's pixels, and what is displayed won't look as good as the original image would. The image would lose some sharpness.
一种情况是您正在显示图像或视频。如果您不对齐设备像素(即视频屏幕的像素),则使用某种算法(插值、抗锯齿)将图像的像素定位在屏幕像素“之间”,显示的内容将不会看起来和原始图像一样好。图像会失去一些清晰度。

