创建用点填充的多边形
问题描述:
正在使用下面的代码创建多边形。我只是想用黑点填充这个多边形表面,我如何做到这一点,然后我想将这个多边形转换为位图或内存流,如何做到这一点?创建用点填充的多边形
// Create a blue and a black Brush
SolidColorBrush yellowBrush = new SolidColorBrush();
yellowBrush.Color = Colors.Transparent;
SolidColorBrush blackBrush = new SolidColorBrush();
blackBrush.Color = Colors.Black;
// Create a Polygon
Polygon yellowPolygon = new Polygon();
yellowPolygon.Stroke = blackBrush;
yellowPolygon.Fill = yellowBrush;
yellowPolygon.StrokeThickness = 4;
// Create a collection of points for a polygon
System.Windows.Point Point1 = new System.Windows.Point(50, 100);
System.Windows.Point Point2 = new System.Windows.Point(200, 100);
System.Windows.Point Point3 = new System.Windows.Point(200, 200);
System.Windows.Point Point4 = new System.Windows.Point(300, 30);
PointCollection polygonPoints = new PointCollection();
polygonPoints.Add(Point1);
polygonPoints.Add(Point2);
polygonPoints.Add(Point3);
polygonPoints.Add(Point4);
// Set Polygon.Points properties
yellowPolygon.Points = polygonPoints;
// Add Polygon to the page
mygrid.Children.Add(yellowPolygon);
答
点是否必须按特定顺序排列,或者您只是想在没有特定顺序的情况下在多边形中使用点状图案?
如果你不需要一个特殊的订单,你可以使用一个刷子,一个DrawingBrush例如。看看这个链接:http://msdn.microsoft.com/en-us/library/aa970904.aspx
然后,您可以将此画笔设置为多边形的填充属性而不是SolidColorBrush。
这是从MSDN链接DrawingBrush例子,但修改以显示点:
// Create a DrawingBrush and use it to
// paint the rectangle.
DrawingBrush myBrush = new DrawingBrush();
GeometryDrawing backgroundSquare =
new GeometryDrawing(
Brushes.Yellow,
null,
new RectangleGeometry(new Rect(0, 0, 100, 100)));
GeometryGroup aGeometryGroup = new GeometryGroup();
aGeometryGroup.Children.Add(new EllipseGeometry(new Rect(0, 0, 20, 20)));
SolidColorBrush checkerBrush = new SolidColorBrush(Colors.Black);
GeometryDrawing checkers = new GeometryDrawing(checkerBrush, null, aGeometryGroup);
DrawingGroup checkersDrawingGroup = new DrawingGroup();
checkersDrawingGroup.Children.Add(backgroundSquare);
checkersDrawingGroup.Children.Add(checkers);
myBrush.Drawing = checkersDrawingGroup;
myBrush.Viewport = new Rect(0, 0, 0.05, 0.05);
myBrush.TileMode = TileMode.Tile;
yellowPolygon.Fill = myBrush;
@的Torsten,不按顺序,只是一个点状图案就是所有,并且我不使用任何XAML代码在这里,只是想创建该图像并保存为位图? – 2010-08-17 08:16:52
@ deep:我编辑了我的帖子,为您提供了一个代码示例。但是,至于把这个多边形保存为位图,我不能给你太多建议。也许这个链接可以帮助你,你通过你的多边形作为可视化参数:http://www.wpftutorial.net/BitmapFromVisual.html – Torsten 2010-08-17 09:13:04
是的工作Torsten,感谢战利品 – 2010-08-17 09:34:28