StreamGeometry combine

后端 未结 2 1050
谎友^
谎友^ 2021-01-24 05:01

I have complex StreamGeometry and I want to clip it. Unfortunately, it looks like StreamGeometry doesn\'t supports combine.

Here is a

2条回答
  •  暖寄归人
    2021-01-24 05:12

    You can definitely combine StreamGeometry objects. For this, you want to use a CombinedGeometry object. Here's a simple extension method for drawing polygons with holes:

    public static void DrawPolygon(this DrawingContext context, Brush brush, Pen pen, IEnumerable exteriorRing, IEnumerable> interiorRings = null)
    {
        StreamGeometry geo = new StreamGeometry();
        Geometry finalGeometry = geo;
    
        using (StreamGeometryContext ctxExterior = geo.Open())
        {
            ctxExterior.BeginFigure(exteriorRing.First(), (brush != null), true);
            ctxExterior.PolyLineTo(exteriorRing.Skip(1).ToArray(), (pen != null), false);
    
            if (interiorRings != null)
            {
                foreach (var ring in interiorRings)
                {
                    var interiorGeometry = new StreamGeometry();
    
                    using (var ctxInterior = interiorGeometry.Open())
                    {
                        ctxInterior.BeginFigure(ring.First(), true, true);
                        ctxInterior.PolyLineTo(ring.Skip(1).ToArray(), (pen != null), false);
                    }
    
                    finalGeometry = new CombinedGeometry(GeometryCombineMode.Exclude, finalGeometry, interiorGeometry);
                }
            }
        }
    
        context.DrawGeometry(brush, pen, finalGeometry);
    }
    

提交回复
热议问题