创建用点填充的多边形

时间:2010-08-17 02:30:13

标签: c# wpf drawing polygons

alt text使用以下代码创建多边形。我只是想用黑点填充这个多边形表面,我怎么做,然后我想将这个多边形转换为位图或内存流,怎么做???

 // 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);

1 个答案:

答案 0 :(得分:3)

点必须按特定顺序定位,还是只想在多边形中使用点状图案而没有特定的顺序?

如果您不需要特殊订单,可以使用Brush,例如DrawingBrush。查看此链接:http://msdn.microsoft.com/en-us/library/aa970904.aspx

然后,您可以将此画笔设置为Polygon的填充属性,而不是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;