Home > View Post

Infinite Resolution and SnapsToDevicePixels

You may have noticed that things can get slightly blurry in WPF rendering with 1 pixel thick strokes clearly spanning more than one pixel. That's because WPF works at infinite resolution (well, as infinite as the double type will allow).

For example, here is a Rectangle positioned on a canvas at 20, 20.

Rectangle at 20, 20

Now look what happens if we move the rectangle down and to the right by just half a 'pixel', placing it at 20.5, 20.5.

Rectangle at 20.5, 20.5

Notice how the horizontal and vertical lines are blurred. This is how that half 'pixel' is represented - by partially occupying the two pixels that the line lies between.

Sometimes this isn't the effect we want and would prefer to stick with a nice crisp line. As usual, the WPF guys catered for us via UIElement's SnapsToDevicePixels property.

<Rectangle Canvas.Top="20.5" Canvas.Left="20.5" Width="10" Height="10" Stroke="Black" SnapsToDevicePixels="true" />

Rectangle at 20.5, 20.5 with SnapsToDevicePixels on

Neat. Best of all, this doesn't disable anti-aliasing. The SnapsToDevicePixels only applies to horizontal and vertical lines, as demonstrated by these two rectangles with rounded corners. The right one has SnapsToDevicePixels set to True.

Rectangle at 20.5, 20.5 with SnapsToDevicePixels on

Tags: WPF

 
Josh Post By Josh Twist
3:06 AM
24 Jul 2007

» Next Post: Inheriting SnapsToDevicePixels
« Previous Post: Sortable ListView in WPF

Comments are closed for this post.

© 2005 - 2017 Josh Twist - All Rights Reserved.