我遇到了 WPF 和 PathGeometry 的问题。我有以下 Path 对象,它是线条和弧线的组合。
<Path Data="M100,180L220,180 M220,180L220,152 M220,152L217,150
M217,150L182,150 M180,147L180,132 M182,130L217,130 M217,130L220,127
M220,127L220,80 M220,80L100,80 M100,80L100,180 M182,150L180,147
M180,132L182,130">
这是一个应该绘制整个形状的简单代码片段。
private void DrawIt()
{
Canvas canv = new Canvas();
this.Content = canv;
canv.Margin = new Thickness(0, 0, 0, 0);
canv.Background = new SolidColorBrush(Colors.White);
Path testPath = GetPath("blue");
testPath.Data = Geometry.Parse("M100,180L220,180 M220,180L220,152 M220,152L217,150 M217,150L182,150 M180,147L180,132 M182,130L217,130 M217,130L220,127 M220,127L220,80 M220,80L100,80 M100,80L100,180 M182,150L180,147 M180,132L182,130 ");
var area = testPath.Data.GetArea();
canv.Children.Add(testPath);
this.Content = canv;
}
private Path GetPath(string aColor)
{
Path imagePath = new Path();
SolidColorBrush colorBrush = (SolidColorBrush)new BrushConverter().ConvertFromString(aColor);
imagePath.Stroke = colorBrush;
imagePath.StrokeThickness = 2;
imagePath.Fill = colorBrush;
return imagePath;
}
上面的代码产生下图:
我的问题是我正在尝试获取此图像的区域,但 Path.Data.GetArea() 只返回 0.0。
任何帮助,将不胜感激。