gpt4 book ai didi

c# - SnapsToDevicePixels 不适用于图像?

转载 作者:太空狗 更新时间:2023-10-30 01:25:28 24 4
gpt4 key购买 nike

我在 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,带有细垂直线:image here . (我的系统分辨率是 96 DPI,Windows XP,.NET 4)

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

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

问题是,如果没有变通办法,为什么 SnapsToDevicePixels="True" 不能工作?是 WPF 中的错误还是我以错误的方式使用它?

最佳答案

从这里blog entry :

SnapsToDevicePixels

WPF anticipated that there would be cases where people wanted to align with the pixel grid instead of using sub-pixel precision. You can set the SnapsToDevicePixels property on any UIElement. This will cause us to try and render to the pixel grid, but there are quite a few cases that don't work - including images. We will be looking to improve this in the future.

因此,这只是 SnapsToDevicePixels 可以执行的已知限制。

关于c# - SnapsToDevicePixels 不适用于图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6925839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com