SnapsToDevicePixels 不适用于图像?
我在 WPF 中遇到位图图像问题。当图像容器从非整数位置开始时,图像似乎不遵循 SnapsToDevicePixels 的值。
示例代码:(
<Window x:Class="BlurryImage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="110" Width="200">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Button SnapsToDevicePixels="True">
<Image SnapsToDevicePixels="True" Source="i16.png" Stretch="None"/>
</Button>
<Button SnapsToDevicePixels="True" Margin="10.333333,0,0,0">
<Image SnapsToDevicePixels="True" Source="i16.png" Stretch="None"/>
</Button>
</StackPanel>
</Window>
注意左边距的值:10.333333。)
这里的图像 i16.png
是一个简单的 16x16 位图,分辨率为 96 DPI,带有细垂直线:。 (我的系统分辨率是96 DPI,Windows XP,.NET 4)
当我运行程序时,第一个图像很清晰,而第二个图像很模糊:
不同的来源(包括 stackoverflow 上的一些来源)提出了不同的解决方法。 (例如,这些帖子:[1] ,<一href="https://stackoverflow.com/questions/592017/my-images-are-blurry-why-isnt-wpfs-snapstodevicepixels-working">[2] 和 [3]。)我尝试了解决方法,它们似乎有效。在主窗口上使用 UseLayoutRounding="true"
可以使两个图像变得清晰。在图像上使用 RenderOptions.BitmapScalingMode="NearestNeighbor" 也可以使其变得清晰。
问题是,为什么没有解决方法 SnapsToDevicePixels="True"
就无法工作?这是 WPF 中的错误还是我以错误的方式使用它?
I've encountered a problem with bitmap images in WPF. When the image container starts on a position which is not a whole number, the image seems to not respect the value of SnapsToDevicePixels
.
Example code:
<Window x:Class="BlurryImage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="110" Width="200">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center">
<Button SnapsToDevicePixels="True">
<Image SnapsToDevicePixels="True" Source="i16.png" Stretch="None"/>
</Button>
<Button SnapsToDevicePixels="True" Margin="10.333333,0,0,0">
<Image SnapsToDevicePixels="True" Source="i16.png" Stretch="None"/>
</Button>
</StackPanel>
</Window>
(Note the value of the left margin: 10.333333.)
Here the image i16.png
is a simple 16x16 bitmap in 96 DPI resolution with thin vertical lines: . (My system resolution is 96 DPI, Windows XP, .NET 4)
When I run the program, the first image is sharp, whereas the second one is blurry:
Different sources, including some here on stackoverflow, suggest different workarounds. (For example, these posts: [1], [2] and [3].) I tried the workarounds, and them seem to work. Using UseLayoutRounding="true"
on the main window makes both images sharp. Using RenderOptions.BitmapScalingMode="NearestNeighbor"
on the image makes it sharp, too.
The question is, why doesn't SnapsToDevicePixels="True"
work without workarounds? Is it a bug in WPF or I am using it in a wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自此博客条目:
因此,这只是 SnapsToDevicePixels 功能的已知限制。
From this blog entry:
So it's just a known limitation of what SnapsToDevicePixels can do.