Draw lines at real pixel position
If you want to draw lines in OnRender function for sample, you have a antialiasing and you can’t have a real position (at pixel)
with this code (find in https://stackoverflow.com/questions/6018106/wpf-drawingcontext-seems-ignore-snaptodevicepixels) you can solve it
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static class SnapDrawingExtensions { public static void DrawSnappedLinesBetweenPoints(this DrawingContext dc, Pen pen, double lineThickness, params Point[] points) { var guidelineSet = new GuidelineSet(); foreach (var point in points) { guidelineSet.GuidelinesX.Add(point.X); guidelineSet.GuidelinesY.Add(point.Y); } var half = lineThickness / 2; points = points.Select(p => new Point(p.X + half, p.Y + half)).ToArray(); dc.PushGuidelineSet(guidelineSet); for (var i = 0; i < points.Length - 1; i = i + 2) { dc.DrawLine(pen, points[i], points[i + 1]); } dc.Pop(); } } |
and code sample
1 2 3 4 5 6 7 8 9 10 11 12 |
protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext) ; Pen p = new Pen(Brushes.Black, 1); List<Point> pts = new List<Point>(); for (double x = 0; x < this.ActualWidth; x+= 10) { pts.Add(new Point(x, 0)); pts.Add(new Point(x, this.ActualHeight)); } drawingContext.DrawSnappedLinesBetweenPoints(p, 1.0, pts.ToArray()); } |
Categories: Non classé
Commentaires récents