我什么时候应该在 WPF 4.0 中使用 SnapsToDevicePixels?
谁能建议有关何时在 WPF 4.0 中使用 SnapsToDevicePixels 的指南?
是否应该仅在出现问题时偶尔使用它,在整个应用程序中广泛使用,仅在某些控件上还是什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Spencer 和 Martin 对于何时对齐像素给出了很好的答案。
至于如何:我还想指出,在 WPF 4.0 中应该尝试使用
UseLayoutRounding
属性的 SnapsToDevicePixels 。UseLayoutRounding
使您所做的事情与 Silverlight 兼容(SnapsToDevicePixels
在 Silverlight 中不可用)...并且 Microsoft 也鼓励使用UseLayoutRounding
在其 上="noreferrer">文档。两者有什么区别?嗯,一个很大的区别是
UseLayoutRounding
发生在布局阶段,而SnapsToDevicePixels
发生在渲染阶段。这让我推测 UseLayoutRounding 可能是一种更高效的方法(不过我还没有证实这一点)。尽管如此,仍然有理由使用 SnapsToDevicePixels。事实上,MSDN 文档指出了其中之一。我还要补充一点:只有使用 SnapsToDevicePixels 才能使用指南进行精确控制。
以下是有关此问题的一些资源(即像素捕捉以及图像、文本和视觉效果的清晰度):
嘿。我知道我的答案比你所要求的要多一点......但是这个概念(即分辨率独立性及其带来的问题以及如何克服它们)通常可以成为 使用 WPF 时的挫败感。至少,我想向您指出新的 WPF 4.0 属性
UseLayoutRounding
。更新
我只需要添加一下,因为我已经看到了这个等等...有时,
SnapsToDevicePixels
可以工作,而UseLayoutRounding
则不起作用。我希望我能弄清楚为什么会出现这种情况,但一定要先尝试 UseLayoutRounding,如果这不起作用,请毫不犹豫地尝试 SnapsToDevicePixels。那条线太锋利了,会割伤你!
Good answers by Spencer and Martin as to the when to align your pixels.
As to the how: I would also point out that one should in WPF 4.0 try using the property
UseLayoutRounding
instead ofSnapsToDevicePixels
.UseLayoutRounding
makes what you are doing compatible with Silverlight (SnapsToDevicePixels
is not available in Silverlight) ... and Microsoft is also encouraging the use ofUseLayoutRounding
overSnapsToDevicePixels
in its documentation.What is the difference between the two? Well, one big difference is that
UseLayoutRounding
occurs during the layout phase whileSnapsToDevicePixels
occurs during the render phase. This makes me speculate thatUseLayoutRounding
is probably a more performant way to go (I haven't confirmed this, though).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 withSnapsToDevicePixels
that you can use guidelines for precise control.Here are some resources on this matter (i.e. pixel snapping and clarity with images, text, and visuals):
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 frustration when working with WPF. At the very least, I wanted to point you to the new WPF 4.0 property,
UseLayoutRounding
.UPDATE
I just have to add since I've seen this over and over ... sometimes
SnapsToDevicePixels
works whenUseLayoutRounding
doesn'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 trySnapsToDevicePixels
.That line is so sharp it can cut you!
一种情况是您正在显示图像或视频。如果您不捕捉设备像素(即视频屏幕的像素),则使用某种算法(插值、抗锯齿)将图像的像素定位在屏幕像素“之间”,并且显示的内容不会看起来像与原始图像一样好。图像会失去一些清晰度。
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.
它应该用在像素放置有意义的控件或区域上。与绘图应用程序的画布相关的控件就是一个例子。您见过碎片驱动器的地图吗?这可能是另一个例子。
我能想到的一个例外是当您使用某种分隔线时。大多数人期望边界线是坚实的。如果关闭此设置,它们可能会看起来模糊且分散注意力。
基本上,如果边缘模糊 = 不好,则将其打开
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
刚刚注意到它对于边界非常有用。其他信息请此处。
Just noticed that it is very usefull for Borders. Additional info here.