我遇到了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。
任何帮助都将不胜感激。
发布于 2014-07-01 22:01:31
您正在描述多个图形,所有这些图形都只是一条直线,因为您一直在发出Move命令。如果您的路径数据是这样的:
M100,180L220,180 220,152 217,150 182,150 180,147 180,132 182,130 217,130 220,127
220,80 100,80 Z相反,它描述了一个单独的图形,它应该绘制与您所显示的相同的形状,但也应该是可填充的。
请参阅Path Markup Syntax
https://stackoverflow.com/questions/24511772
复制相似问题