My images are blurry! Why isn't WPF's Snap

2019-01-03 04:15发布

I'm using some Images in my WPF applcation.

XAML:

<Image Name="ImageOrderedList"
       Source="images/OrderedList.png"
       ToolTip="Ordered List"
       Margin="0,0,5,5"
       Width="20"
       Height="20"
       SnapsToDevicePixels="True"
       MouseUp="Image_MouseUp"
       MouseEnter="Image_MouseEnter"
       MouseLeave="Image_MouseLeave" />

But, they appear fuzzy:

Dead link - Blurry WPF Images

Here's a zoomed-in, side-by-side comparison. An original is on the left:

Dead link - Blurry WPF Image Zoomed

Why doesn't that SnapsToDevicePixels="True" line prevent this problem?

12条回答
劫难
2楼-- · 2019-01-03 04:28

You may want to consider trying a new property available now in WPF4. Leave the RenderOptions.BitmapScalingMode to HighQuality or just don't declare it.

NearestNeighbor worked for me except it led to jaggy bitmaps when zooming in on the application. It also didn't seem to fix any glitches where icons were sizing in weird ways.

On your root element (i.e. your main window) add this property: UseLayoutRounding="True".

A property previously only available in Silverlight has now fixed all Bitmap sizing woes. :)

查看更多
beautiful°
3楼-- · 2019-01-03 04:28

Rather than using SnapsToDevicePixels, I instead used RenderOptions.BitmapScalingMode and they're now nice and crisp!

XAML:

<Image Name="ImageOrderedList"
       Source="images/OrderedList.png"
       ToolTip="Ordered List"
       Margin="0,0,5,5"
       Width="20"
       Height="20"
       RenderOptions.BitmapScalingMode="NearestNeighbor"
       MouseUp="Image_MouseUp"
       MouseEnter="Image_MouseEnter"
       MouseLeave="Image_MouseLeave" />

Here's how it now looks:

Crisp WPF Images http://img13.imageshack.us/img13/9926/crispwpfimages.gif

查看更多
神经病院院长
4楼-- · 2019-01-03 04:36

+1 for Zack Peterson

I'm using .Net 3.5 sp1 and it looks like the most simple solution for a large number of fuzzy images. It's not a big deal to specify RenderOptions in-place, but for 3rd-party components a style in app-level resource makes sense:

 <Style TargetType="{x:Type Image}">
    <Setter
        Property="RenderOptions.BitmapScalingMode"
        Value="NearestNeighbor" />
 </Style>

Worked nicely when AvalonDock started to render blurry icons.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-03 04:37

Make sure you save the image in the same DPI as your WPF application is working in, some image formats have this info stored as metadata. I don't know if this solves the problem but I've hade some problems because of this where images resized to 100% got bigger or smaller than expected.

Might be something similar.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-03 04:37

I have found that the RenderOptions.BitmapScalingMode="NearestNeighbor" does not work for me. I'm using Windows XP x32 with DirectX 9.0c. As the actual rendering for WPF is done with DirectX, this could have an effect. I do have anti-aliasing turned on for XP with the following registry entries:

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Avalon.Graphics] "MaxMultisampleType"=dword:00000004 "EnableDebugControl"=dword:00000001

However, turning aa off with these settings has no effect on the images. I think this only effects 3D Viewports.

Finally, I found that the blurring occurs with the text of TextBlocks as well as images. And the blurring only happens for some text blocks and images, not all of them.

查看更多
▲ chillily
7楼-- · 2019-01-03 04:38

I believe this is a bug (or at least it was). Check out this Microsoft support e-mail exchange page for some ideas to fix it.

查看更多
登录 后发表回答