SnapsToDevicePixels不适用于图像?

时间:2011-08-03 11:39:37

标签: c# wpf bitmap

我在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是96 DPI分辨率的简单16x16位图,具有细垂直线:image here。 (我的系统分辨率是96 DPI,Windows XP,.NET 4)

当我运行程序时,第一张图像很清晰,而第二张图像模糊:blurry image screenshot

不同的来源,包括stackoverflow上的一些来源,提出了不同的解决方法。 (例如,这些帖子:[1][2][3]。)我尝试了解决方法,它们似乎有效。在主窗口上使用UseLayoutRounding="true"可使两个图像清晰。在图像上使用RenderOptions.BitmapScalingMode="NearestNeighbor"也会使其变得清晰。

问题是,为什么SnapsToDevicePixels="True"没有解决方法?它是WPF中的错误还是我以错误的方式使用它?

1 个答案:

答案 0 :(得分:4)

从此blog entry

  

<强> SnapsToDevicePixels

     

WPF预计会出现人们想要调整的情况   使用像素网格而不是使用子像素精度。你可以设置   任何UIElement上的SnapsToDevicePixels属性。这将导致我们   尝试渲染到像素网格,但有很多情况   不起作用 - 包括图像。我们将寻求改进   这将来。

所以这只是SnapsToDevicePixels可以做的一个已知限制。