简体   繁体   中英

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

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);

Two things:

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

Try adding this:

myLine.SnapsToDevicePixels = true;

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

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.

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 -- ie reliably two pixels wide!

        x = (int)x;

Adding 0.5 to that made it reliably one pixel:

        x = (int)x + 0.5;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM