如何绘制多边形?(在C#WPF项目中)

时间:2013-10-07 07:52:21

标签: c# wpf polygon polyline

点击我想从图像上的多边形区域制作的点。

myPolygon = new Polygon();
myPolygon.Stroke = Brushes.Black; 
myPolygon.Fill = Brushes.LightYellow; 
myPolygon.StrokeThickness = 2; 
myPolygon.HorizontalAlignment = HorizontalAlignment.Left; 
myPolygon.VerticalAlignment = VerticalAlignment.Center; 
myPolygon.PreviewMouseLeftButtonDown += new MouseButtonEventHandler(Polygon_MouseDown); 
myPolygon.PreviewMouseLeftButtonUp += new MouseButtonEventHandler(Polygon_MouseUp);     

private void Polygon_MouseDown(object sender, MouseButtonEventArgs e) 
{     
    Point p = e.GetPosition(image); 
    myPolygon.Points = new PointCollection() { new Point(p.X,p.Y) };
    RootCanvas.Children.Add(myPolygon); 
} //MouseClick Event BUT, did not click behavior.. I want draw a line along the points.

我该怎么办??

1 个答案:

答案 0 :(得分:10)

我们可以使用WPF画布绘制Polygon,它是一个子对象的集合。

Polygon p = new Polygon();
p.Stroke = Brushes.Black;
p.Fill = Brushes.LightBlue;
p.StrokeThickness = 1;
p.HorizontalAlignment = HorizontalAlignment.Left;
p.VerticalAlignment = VerticalAlignment.Center;
p.Points = new PointCollection() { new Point(10, 10), new Point(100, 100), new Point(200, 200) };
freeCanvas.Children.Add(p);

欲了解更多信息,请参阅以下网址

http://www.codeproject.com/Articles/128705/WPF-rounded-corners-polygon

http://classicalprogrammer.wikidot.com/draw-dynamic-polygons-in-wpf

http://msdn.microsoft.com/en-us/library/ms747393.aspx