Q:

How can I get DrawGeometry to draw along device pixel lines in WPF?

I'm trying to use DrawingContext.DrawGeometry get a rectangle with 1 device pixel wide sides.
var geometry = (defined elsewhere from a Rect {0.0, 0.0, 8.0, 18.0})
var brush = new SolidColorBrush(Color.FromArgb(30, 0xff, 0xff, 0xff));

// Note: the pen is declared with a 1.0 thickness
var pen = new Pen(new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF)), 1.0)

drawingContext.DrawGeometry(brush, pen, geometry);

I have verified that the Rect I'm building the geometry with is whole numbers, not fractional points (manually verified), and the Pen I'm drawing the geometry with is 1.0 units wide.
The problem is that the rect is being drawn on screen 2 device pixels wide. How can I draw this rectangle on exact device pixels? I'm expecting that at 96dpi, this will come out to 1 pixel.

A:

You can use DrawingContext.PushGuidelineSet to force the rendering to align when possible.
For details and sample code, see this tutorial.

