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

  • Question


    The method for drawing a line on a canvas in WPF that uses the line class actually draws a line that is two pixels 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;

    Microsoft might have decided to set a standard for line thickness and the minimum is 2 pixels thick when you set the strockThickness to 1, but when you already have rectangles drawn in XAML and even error fonts using WingDings, it is an obvious miss-match.  How do you draw a line that is truly 1 pixel thick?


    Web Master and Programmer
    Friday, May 21, 2010 1:26 AM


  • This worked:

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


    Web Master and Programmer
    • Proposed as answer by Olaf Rabbachin Friday, May 21, 2010 7:31 AM
    • Marked as answer by Jie Bao Monday, May 24, 2010 10:30 AM
    Friday, May 21, 2010 6:51 AM