170

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.

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

3
  • 1
    blogs.msdn.com/dwayneneed/archive/2007/10/05/… Commented Feb 27, 2009 at 2:34
  • 4
    Your image links seem to have broken. If you still have the original images, please reupload them to stack.imgur. Thanks. Commented Jul 27, 2015 at 8:42
  • 1
    If none of the tips below work immediately, also try and change the size of your image to a factor of 4 in width and height. So instead of 179 X 44, try 176 X 44. Commented Aug 29, 2017 at 20:44

12 Answers 12

258

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. :)

Sign up to request clarification or add additional context in comments.

8 Comments

More information on this new property found here: blogs.msdn.com/text/archive/2009/08/27/layout-rounding.aspx
UseLayoutRendering="True" is what I used - this is perfect for solving my blurry images. Thanks!
FINALLY!! UseLayoutRounding should be set by default. Images show up just like the original and even text in some places (like ContextMenus, for me at least) shows up crisper than before. Thanks, Domokun!
I guess those of us still stuck on .NET 3.5 have no options?
I'm finding that this fixes my problem if I set the property Stretch to None on images, but in all other scenarios, even with HighQuality image rendering and aliasing turned off, image stretching still sucks in WPF. But, at least this has fixed the problem for non-stretched images (which shouldn't have been a problem in the first place)
|
79

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" /> 

10 Comments

Also if your image was the exact size as specified in the <Image> tag, then it wouldn't have to scale it and should render it crisply.
I'm not sure this will have the desired effect at a different DPI
Beardo, both the source graphic and the <Image> are both 20 pixels by 20 pixels. As I understand it, the issue comes from WPF. It sort-of wants to disregard the pixel grid of the monitor, so it's logical grid usually won't perfectly line up with the physical grid.
@Zack Width="20" does not mean 20 pixels. It means 20/96 of an inch. If your OS is configured to run at 96 DPI then it is 20 pixels. Now how will your nearest neighbor look on a good monitor, 160 DPI for instance? And how will it look when you print at 300 DPI? You shouldn't optimize for your dev machine.
I also found that for pixel-sized images NearestNeighbor is much better than HighQuality, especially if you combine it with img.Width = imgSource.PixelWidth; img.Height = imgSource.PixelHeight. My client provided some images with different crazy DPI values and I couldn't ask the client to convert them all, so I had to use this hack.
|
23

+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.

1 Comment

AvalonDock is also giving me the same headaches ... and I'm still with .Net 3.5
10

Using the UseLayoutRounding="True" on the root Window works in many cases but I encountered a problem when using the WPF Ribbon control. My application relies on Contextual Tabs that appear according to what the user is doing and when I set the UseLayoutRounding to True, the contextual tab would not show up and the RibbonButton's image neither. Also, the application freezes for many seconds and CPU fan starts to sing.

Using RenderOptions.BitmapScalingMode="NearestNeighbor" on my image corrected the image rendering issues (fuzzy and cropped image) and is fully compatible with the Ribbon Contextual Tabs usage.

1 Comment

UseLayoutRounding="True" worked for me. Thanks. mikecroteau.wordpress.com/2013/01/20/wpf-net-xaml-blurry-images
6

RenderOptions.BitmapScalingMode="NearestNeighbor" works well most of the time. However, occasionally you'll get graphical glitches (in my case, 4 out of 5 images showed up fine, but the fifth had a slight distortion on the right edge). I fixed it my increasing the Image control's right margin by 1.

If that still doesn't fix it, try the Bitmap class control above that EugeneZ mentions. It's a replacement for the Image control and so far it's worked pretty well for me. See http://blogs.msdn.com/dwayneneed/archive/2007/10/05/blurry-bitmaps.aspx

Comments

6

use UseLayoutRounding=True to the top most element in your application

Comments

5

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.

Comments

3

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.

Comments

3

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.

Comments

3

I have found that no combination of the suggested workarounds would cure my seemingly random blurry image problem. I like many others cannot upgrade to .net 4 in order to use the UseLayoutRendering property.

What I have found to work:

  • Ensure your [original] image dimensions are multiples of 2. This seems to prevent some of the funky image scaling problems.
  • Sometimes I have also found that adjusting margins on images by a pixel or 2 can prevent the problem.

Comments

1

My first thought, reading the question, was you were blowing up the image too much, but that does not appear to be the case looking at the image you have of the app.

Second thought is color palette, but with black as one of the colors that is not rendering correctly, this is not as likely.

If you can fully rule out the two above, I am currently stumped.

As an experiment, you can try other graphics formats, but PNG should be fine. I will have to think it through some more to come up with a better answer.

1 Comment

+1 to ward off unwarranted negative votes since I think you offered some reasonable suggestions and were only trying to help and most importantly there wasn't anything incorrect with your suggestions.
1

I've tried to use the RenderOptions.BitmapScalingMode=HighQuality, seems like is causes some problems in Windows 8.1, so what i did was to run them through the tool called PngOut.exe

http://advsys.net/ken/utils.htm

Which reduces the header of the png, and also reduces the size, but without changing the image quality.

And now all my images are perfect! :-)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.