我在 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>
Run Code Online (Sandbox Code Playgroud)
(注意左边距的值:10.333333。)
这里的图像i16.png是一个简单的 16x16 位图,分辨率为 96 DPI,带有细垂直线:
. (我的系统分辨率是 96 DPI、Windows XP、.NET 4)
当我运行程序时,第一个图像清晰,而第二个图像模糊: 
不同的来源,包括 stackoverflow 上的一些,建议了不同的解决方法。(例如,这些帖子:[1]、[2]和[3]。)我尝试了解决方法,它们似乎有效。使用UseLayoutRounding="true"主窗口上,使两个图像锐利。RenderOptions.BitmapScalingMode="NearestNeighbor"在图像上使用也使其清晰。
问题是,为什么SnapsToDevicePixels="True"没有变通办法就行不通?这是 WPF 中的错误还是我以错误的方式使用它?