When should you use SnapsToDevicePixels in WPF 4.0?

Can anyone suggest a guide on using SnapsToDevicePixels in WPF 4.0?

Should it be used only occasionally, if there is a problem, liberally in the application, only on certain controls or what?

+45
wpf
Mar 08 '10 at 6:48
source share
4 answers

Good answers from Spencer and Martin to at to align your pixels.

As for how : I would also like to point out that in WPF 4.0 try using the UseLayoutRounding property instead of SnapsToDevicePixels .

UseLayoutRounding does what you do compatible with Silverlight ( SnapsToDevicePixels not available in Silverlight) ... and Microsoft also encourages the use of UseLayoutRounding over SnapsToDevicePixels in its own.

What is the difference between the two? Well, the big difference is that UseLayoutRounding happens during the UseLayoutRounding phase, and SnapsToDevicePixels happens during the render phase. This makes me suggest that UseLayoutRounding is probably a more efficient way to go (I haven't confirmed this yet).

All that said, there will still be reasons to use SnapsToDevicePixels . In fact, the MSDN documentation points to one. I will add another: only with SnapsToDevicePixels can you use recommendations for precise control.

Here are some resources on this subject (for example, pixel snapping and sharpness with images, text, and visual effects):

Heh. I know that my answer was a little more than what you asked for ... but this concept (i.e. independence from the resolution and the resulting problems that it causes and how to overcome them) can often be a point of frustration at work with WPF. At least I wanted to point you to a new WPF 4.0 property, UseLayoutRounding .

UPDATE

I just need to add since I saw it again and again ... sometimes SnapsToDevicePixels works when UseLayoutRounding not. I'm sorry I can't say why this is, but try UseLayoutRounding first, and if that doesn't work, feel free to try SnapsToDevicePixels .

This line is so sharp that it can cut you off!

+71
Apr 08 '10 at 17:12
source share

It should be used for controls or areas where pixel placement occurs. One example is the control associated with the canvas of a drawing application. Have you ever seen a fragmented disk card? This could be another example.

The only exception I can think of is when you use some kind of dividing lines. While most people expect borders to be solid, if this option is turned off, they may look blurry and distracting.

Basically, if the blurry edges = bad, then turn it off.

+8
Mar 08 '10 at 7:02
source share

One case is if you are displaying an image or video. If you are not attached to the pixels of the device (that is, to the pixels of the video screen), then some algorithm (interpolation, smoothing) is used to position your image pixels β€œbetween” your screen pixels, and the displayed image will not look the same as the original image . The image will lose some sharpness.

+5
Mar 08 '10 at 7:00
source share

Just noticed that it is very useful for Borders. More info here .

 <Style TargetType="Border" > <Setter Property="SnapsToDevicePixels" Value="True" /> </Style> 
+1
Jun 12 '15 at 14:46
source share



All Articles