Keep in mind that (0, 0)
is not the center of the upper left pixel. Instead it is the upper left corner of that pixel. In order to draw a line with a stroke thickness of 1
in the middle of the second pixel column (with index 1) you would have to draw from (1.5, 1)
to (1.5, RenderSize.Height - 1)
:
dc.DrawLine(myPen, new Point(1.5, 1), new Point(1.5, RenderSize.Height - 1));
Setting SnapsToDevicePixels = true
made your line snap to the left by half a pixel.
If you would use PenLineCap.Square for both the StartLineCap
and EndLineCap
properties of the line's Pen, you could draw from exactly one pixel center to the other:
var myPen = new Pen(Brushes.LightGray, 1);
myPen.StartLineCap = PenLineCap.Square;
myPen.EndLineCap = PenLineCap.Square;
dc.DrawLine(myPen, new Point(1.5, 1.5), new Point(1.5, RenderSize.Height - 1.5));