Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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); 
like image 411
xarzu Avatar asked May 21 '10 01:05

xarzu


People also ask

How do I draw a line in canvas WPF?

To draw a line, create a Line element. Use its X1 and Y1 properties to set its start point; and use its X2 and Y2 properties to set its end point. Finally, set its Stroke and StrokeThickness because a line without a stroke is invisible. Setting the Fill element for a line has no effect, because a line has no interior.

What is the use of canvas in WPF?

Canvas panel is the basic layout Panel in which the child elements can be positioned explicitly using coordinates that are relative to the Canvas any side such as left, right, top and bottom.

What is canvas XAML?

XAML attached properties. Canvas is the host service class for several XAML attached properties. In order to support XAML processor access to the attached properties, and also to expose equivalent get and set operations to code, each XAML attached property has a pair of Get and Set accessor methods.


1 Answers

Two things:

myLine.SnapsToDevicePixels = true; myLine.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased); 
like image 180
Rusty Avatar answered Oct 25 '22 14:10

Rusty