How do you draw a line on a canvas in WPF that is 1 pixel thick

我的未来我决定 提交于 2020-01-10 12:05:40

问题


I'm using the Line class to draw on a canvas in WPF and even though I set StrokeThickness = 1, the line shows up 2 pixels wide - it's almost as if the minimum thickness is two. How do I draw a line that is truly 1 pixel thick?

Line myLine = new Line();

myLine.Stroke = System.Windows.Media.Brushes.Black;

myLine.X1 = 100;
myLine.X2 = 140;  // 150 too far
myLine.Y1 = 200;
myLine.Y2 = 200;

myLine.StrokeThickness = 1;

graphSurface.Children.Add(myLine);

回答1:


Two things:

myLine.SnapsToDevicePixels = true;
myLine.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);



回答2:


Try adding this:

myLine.SnapsToDevicePixels = true;

That will stop WPF from rendering "half pixels" to antialias your line.




回答3:


Apart from what has been suggested, it might also be possible that your screen resolution is more than 96 DPI. Whatever measurements you are giving to WPF, by default WPF will always assume that 96 pixels corresponds to 1 inch.

The result is, on a screen of say 192 DPI (96 * 2), drawing a line of thickness 1 will draw a line with thickness of 2 pixels.

If this is the case, you will have to explicitly specify the units.




回答4:


I found that this answer wasn't sufficient, that it doesn't work for vertical lines on a Canvass: which sometimes displayed as 2 pixels wide. To fix that I find I need to constrain the X-position of the line

I used the following method in a subclass of Canvass:

    Line newLine(double x1, double x2, double y1, double y2, Brush brush)
    {
        Line line = new Line();
        line.X1 = x1;
        line.X2 = x2;
        line.Y1 = y1;
        line.Y2 = y2;

        line.StrokeThickness = 1;
        line.Stroke = brush;

        // https://stackoverflow.com/questions/2879033/how-do-you-draw-a-line-on-a-canvas-in-wpf-that-is-1-pixel-thick
        line.SnapsToDevicePixels = true;
        line.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

        base.Children.Add(line);

        return line;
    }

    internal void ShowVertical(double x)
    {
        Line line = newLine(0, 0, 50, 150, Brushes.Red);
        SetLeft(line, x);
    }

This was unreliable: the line sometimes displayed as one pixel wide, and sometimes two pixels.

Constraining the x value to an integer made it reliable -- i.e. reliably two pixels wide!

        x = (int)x;

Adding 0.5 to that made it reliably one pixel:

        x = (int)x + 0.5;


来源:https://stackoverflow.com/questions/2879033/how-do-you-draw-a-line-on-a-canvas-in-wpf-that-is-1-pixel-thick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!