【问题标题】:create polygon filled up with dots创建用点填充的多边形
【发布时间】:2010-08-17 02:30:13
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf drawing polygons


    【解决方案1】:

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

    如果您不需要特殊订单,您可以使用画笔,例如绘图画笔。看看这个链接: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;
    

    【讨论】:

    • @Torsten,不按顺序,只是一个点状图案而已,这里没有使用任何 XAML 代码,只是想创建该图像并保存为位图???
    • @deep: 我编辑了我的帖子给你一个代码示例。但是,关于将这个多边形保存为位图,我不能给你太多建议。也许此链接可以帮助您,您将 Polygon 作为 Visual 参数传递:wpftutorial.net/BitmapFromVisual.html
    • @Torsten,我尝试将其作为视觉参数发送,它可以工作,但保存的图像是空白的...请告诉我如何将其转换为位图或内存流???跨度>
    • @deep 你能用你当前使用的代码更新你的问题,看看你是如何保存图像的吗?
    • @Limo Wan Kenobi deep 针对这个问题提出了一个新问题:stackoverflow.com/questions/3509296/wpf-polygon-to-bitmap
    猜你喜欢
    • 1970-01-01
    • 2020-10-16
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多