Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

Layout Rounding

Pete Brown - 08 April 2010

WPF and Silverlight both operate on a subpixel grid. That is, you can align elements on partial pixel boundaries, like this:

<Rectangle Margin="10.5 10.5 10 10"
           Stroke="Black"
           StrokeThickness="1"
           Height="100"
           Width="300" />

Unfortunately, for single-pixel-width elements, the result is usually not what you want. For example, the xaml above produces this rectangle:

image

You may be able to tell it's a little fuzzy. Here's a zoomed-in view of the upper left corner:

image

No, probably not what you want. You specified a 1px line. Sure, you could have simply started it on a whole pixel, but while that is easy in this instance, when you have nested containers inside elements inside panels inside windows, all with their own margins, it gets pretty complicated.

WPF now has a great UI feature that Silverlight got back in the SL3 timeframe: Layout Rounding. Layout rounding lets you specify that you want the shape to snap to the closest whole pixel. Rather than have a double-width gray line, you'll get a single-width black line, but it may be a partial pixel off from where you thought it would be. That's the trade-off, and it's one that many folks are willing to make.

<Rectangle Margin="10.5 10.5 10 10"
           UseLayoutRounding="True"
           Stroke="Black"
           StrokeThickness="1"
           Height="100"
           Width="300" />

Here's the same rectangle with layout rounding turned on.

image

And here's the zoomed-in shot:

image

Now that's more like it!

Anti-aliasing has its place, for sure, but when you want crisp lines, you want crisp lines. I'm happy to see this addition to WPF 4.

       
posted by Pete Brown on Thursday, April 8, 2010
filed under:        

7 comments for “Layout Rounding”

  1. Josh Einsteinsays:
    @Kiril, actually WPF 4 gets UseLayoutRounding for consistency with Silverlight. But Pete, perhaps an explanation about the difference between SnapsToDevicePixels and UseLayoutRounding would be helpful. To be honest, I don't know the difference myself although I suspect it's basically the same except that the former happens in the rendering pass while the latter happens in the layout pass?
  2. Petesays:
    Use of SnapToDevicePixels is not longer recommended for achieving pixel-perfect lines in WPF 4. There's actually a note on the MSDN doc page for SnapToDevicePixels.

    As Josh said, UseLayoutRouting is a layout pass modifier whereas SnapToDevicePixels is calculated at render time. I haven't tested anything, but I would assume you'd see some differences when animating, as well as potentially some perf differences between the two, depending on how many times layout is called vs. render.

    UseLayoutRounding is also Silverlight-compatible :)
  3. Cory Plottssays:
    This must be in the air ... I just answered a <a href="http://stackoverflow.com/questions/2399731/when-should-i-use-snapstodevicepixels-in-wpf-4-0/2601952#2601952">question</a> at StackOverflow on the subject.

    Nice post Pete ... glad to see some attention on the matter. Clean, crisp lines really help an app stand out.

Comment on this Post

Remember me