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);
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.
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.
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.
Two things:
myLine.SnapsToDevicePixels = true; myLine.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With