StreamGeometry combines

I have a complex StreamGeometry and I want to trim it. Unfortunately, it seems that StreamGeometry does not support the compiler.

Here is a test.

Xaml:

 <Path x:Name="path" Stroke="Red"/> 

the code:

 var clip = new RectangleGeometry(new Rect(50, 50, 10, 10)); var rect = new RectangleGeometry(new Rect(0, 0, 100, 100)); var stream = new StreamGeometry(); using (var geometry = stream.Open()) { geometry.BeginFigure(new Point(0, 0), false, true); geometry.LineTo(new Point(0, 100), true, false); geometry.LineTo(new Point(100, 100), true, false); geometry.LineTo(new Point(100, 0), true, false); } //path.Data = rect; //path.Data = stream; //path.Data = clip; //path.Data = Geometry.Combine(rect, clip, GeometryCombineMode.Intersect, null); //path.Data = Geometry.Combine(stream, clip, GeometryCombineMode.Intersect, null); 

Uncommenting the first line (to display rect ) or the second line (to display stream ) gives the same visual result:

An uncommenting third line (to show clip ) or a fourth line (to show the intersection of clip and rect ) will produce:

While uncommenting the last line (to display the intersection of clip and geometry ) creates a blank screen.

My question is: how to combine (clip) StreamGeometry ?

I know there is UIElement.Clip , but:

 // blank screen path.Data = stream; path.Clip = clip; // blank screen as well path.Data = stream; path.Clip = clip; 
+1
source share
2 answers

The solution is simple: do not use StreamGeometry .

For example, this will work (use PathGeometry ):

 var clip = new RectangleGeometry(new Rect(50, 50, 10, 10)); var geometry = new PathGeometry(new[] { new PathFigure(new Point(0, 0), new[] { new LineSegment(new Point(0, 100), true), new LineSegment(new Point(100, 100), true), new LineSegment(new Point(100, 0), true), }, true) }); path.Data = Geometry.Combine(clip, geometry, GeometryCombineMode.Intersect, null); 

Result:

Very important!

It looks like UIElement.Clip still displays invisible parts (maybe only with StreamGeometry )! Never use it! Geometry of the clip before assigning it.

+1
source

You can combine StreamGeometry objects. For this, you want to use the 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<Point> exteriorRing, IEnumerable<IEnumerable<Point>> 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); } 
+1
source

Source: https://habr.com/ru/post/1204106/


All Articles