2010-03-21 155 views
0

我有一个画布,并在其中我画一些线路: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); 
+3

这段代码是怎么被执行的? *我*从不大于或等于100. – Anzurio 2010-03-21 04:58:40

+0

我无法用您在此处解释的简单代码(滚动查看器中的画布)重现问题。我怀疑还有其他事情正在引发你的问题......你能扩展你的例子吗? – 2010-03-21 13:12:20

+0

@stefan:通过编辑你的问题来更新你的问题。我已经从下面删除了更新(只有您的问题的答案应该在那里),并将它们放在“更新”文本下的问题中。 – Sampson 2010-03-22 15:56:21

回答

2

设置路径属性SnapsToDevicePixels为true。

+0

谢谢你的回复。我在格式化方面遇到了一些问题,输入了一些新内容,最后条件(仅在文章中)错了,对不起。真的没有更多的代码,可以使用myPath.SnapsToDevicePixels = true修复问题; 非常感谢任何帮助! – 2010-03-21 15:59:29

+0

哦对不起,我没有看到答案,有点混淆,有评论和答案。 – 2010-03-21 18:26:22

+0

如果我提出的解决方案与您的解决方案匹配,如果您接受它,我会很高兴。 :-) – 2010-03-21 19:19:38

相关问题