2

替代文字我正在使用下面的代码来创建多边形。我只想用黑点填充这个多边形表面,我该怎么做,然后我想把这个多边形转换为位图或内存流,怎么做?

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

1 回答 1

3

点是否必须以特定顺序定位,或者您只想在多边形中使用点图案而没有特定顺序?

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

然后,您可以将此 Brush 设置为 Polygon 的 Fill-Property,而不是 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;
于 2010-08-17T07:18:58.090 回答