Hello,
I have a canvas and in it I'm drawing some lines:
for (int i = 1; i >= 100; i++)
{
// Line
LineGeometry line = new LineGeometry();
line.StartPoint = new Point(i * 100, 0);
line.EndPoint = new Point(i * 100, 100 * 100);
// Path
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 1;
// Add to canvas
myPath.Data = line;
canvas1.Children.Add(myPath);
}
Well, nothing special, but it makes problems - the lines are drawn different! The canvas is inside a scrollviewer, the following image shows different positions of the canvas, however the lines should look the same? Hell, how is this possible? The code above is the only code I've written by hand and it's the only content in the canvas. Anyone knows why this happens and how to prevent this? Thank you very much! Screenshot: http://www.imagebanana.com/view/c01nrd6i/lines.png
Update
So maurizios solution works fine, but only as long as you use the LineGeometry to position the line, I still get blurry lines when I use the canvas to position the line. Anyone can please help me with this? Thank you very much!
Sample screenshot and the code for it:
Screenshot: http://www.imagebanana.com/view/lu7z3mcv/canvasposprob.png
Code:
// LINE POSITIONED BY CANVAS -> LINE GETS BLURRY
// Line
LineGeometry line = new LineGeometry();
line.StartPoint = new Point(0, 0);
line.EndPoint = new Point(0, 100);
// Path
Path myPath = new Path();
myPath.Stroke = Brushes.Black;
myPath.StrokeThickness = 5;
myPath.SnapsToDevicePixels = true;
line.Freeze();
// Add to canvas
myPath.Data = line;
Canvas.SetLeft(myPath, 10); // LINE POSITIONED BY CANVAS: x=10
Canvas.SetTop(myPath, 0); // LINE POSITIONED BY CANVAS
canvas1.SnapsToDevicePixels = true; // doesnt help
canvas1.Children.Add(myPath);
// LINE POSITIONED BY LINE GEOMETRY
// Line
LineGeometry line2 = new LineGeometry();
line2.StartPoint = new Point(20, 0); //LINE POSITIONED BY LINE GEOMETRY: x=20
line2.EndPoint = new Point(20, 100);
// Path
Path myPath2 = new Path();
myPath2.Stroke = Brushes.Blue;
myPath2.StrokeThickness = 5;
myPath2.SnapsToDevicePixels = true;
line2.Freeze();
// Add to canvas
myPath2.Data = line2;
// Don't use the canvas for positioning
//Canvas.SetTop(myPath, 10); //NEW
//Canvas.SetLeft(myPath, 10); //NEW
canvas1.Children.Add(myPath2);