我有一个画布,并在其中我画一些线路:WPF:同一条线绘制不同?
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);
}
好了,没有什么特别,但它使问题 - 线被描绘不同!画布位于scrollviewer内部,下图显示了画布的不同位置,但行应与看起来相同?地狱,这怎么可能?上面的代码是我手写的唯一代码,它是画布中唯一的内容。任何人都知道为什么发生这种情况,以及如何防止这种情况?非常感谢你! 截图:http://www.imagebanana.com/view/c01nrd6i/lines.png
更新
所以maurizios解决方案工作正常,但只要只有当你使用LineGeometry来定位线,我仍然得到模糊的线条,当我使用画布来定位线。 任何人都可以请帮我一下吗?非常感谢你!
样品截图和它的代码:
截图:http://www.imagebanana.com/view/lu7z3mcv/canvasposprob.png
代码:
// 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);
这段代码是怎么被执行的? *我*从不大于或等于100. – Anzurio 2010-03-21 04:58:40
我无法用您在此处解释的简单代码(滚动查看器中的画布)重现问题。我怀疑还有其他事情正在引发你的问题......你能扩展你的例子吗? – 2010-03-21 13:12:20
@stefan:通过编辑你的问题来更新你的问题。我已经从下面删除了更新(只有您的问题的答案应该在那里),并将它们放在“更新”文本下的问题中。 – Sampson 2010-03-22 15:56:21