WPF:相同的线条绘制不同?

时间:2010-03-21 04:18:08

标签: c# 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);
}

嗯,没有什么特别的,但它会产生问题 - 线条被绘制出来了!画布位于滚动查看器中,下图显示了画布的不同位置,但看起来是否相同?天啊,这怎么可能?上面的代码是我手工编写的唯一代码,它是画布中唯一的内容。任何人都知道为什么会发生这种情况以及如何预防非常感谢你! 屏幕截图: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);

1 个答案:

答案 0 :(得分:2)

将Path属性SnapsToDevicePixels设置为true。