I am seeing the weirdest bug with the following code.
I have a PathGeometry to which I added a PathFigure so that I can add LineSegments to it.
This is what I do:
_pathGeometry.Figures.Add(_pathFigure);
_pathFigure.StartPoint = new Point(4, 0);
LineSegment lineSegment1 = new LineSegment(new Point(4, -10), true);
LineSegment lineSegment2 = new LineSegment(new Point(4, 0), true);
_pathFigure.Segments.Add(lineSegment1);
_pathFigure.Segments.Add(lineSegment2);
I then draw it:
using (DrawingContext drawingContext = RenderOpen())
drawingContext.DrawGeometry(null, _pen, _pathGeometry);
What I should see:
WPF should draw a vertical line that goes from 0 to -10 and back to 0. The last part (back to 0) cannot be seen because it’s drawn on the same x pixel. But the last part causes the following:
What I see:
WPF draws a line that goes from 0 to -15. It makes no sense to me. This 5 pixel difference happens whenever I draw a vertical line on top of another vertical line as in the previous example.
Please someone tell me I made a mistake and this is not a WPF bug.
I think the issue has to do with how WPF renders “corners” in your Path. As the angle between two line segments becomes more acute, the corner rendering becomes more apparent.
In your case, you have a zero degree angle (a line segment folding back on itself), which is the most problematic case.
Not all is lost — there several possible solutions:
For more information regarding StrokeLineJoin, see here. For an interesting post about how WPF renders Mitered corners, see here.