Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping an arbitrary wpf geometry

The background to my problem is that I have a bunch of geometries (huge amount, think map over a larger area) split across multiple wpf geometry instances (originally they were PathGeometry, but to reduce memory usage I pre-process them and create StreamGeometries during load). Now what I want to do is to generate tiles from these geometries.

Basically I would like to take a larger geometry object and "cut out" a rectangle of it (my tile) so I get several smaller geometries. Something like the image below:

Slicing up a large geometry

Notice that I want the result to be a new geometry, not a rendering. I know I can achieve the visual result by applying a clip to a UIElement or by pushing a clip to a drawingvisual.

I've tried using Geometry.Combine with one of the arguments being the clip rectangle, but I can't get it to do what I want (I typically only get the clip rect back, or an empty geometry, depending on which combine mode I use).

Alternatively, if this cannot be done using WPF, is there any other (third party is ok) general purporse geometry API for .NET that can do these kind of operations? Or maybe this can be implemented using other parts of the WPF geometry API?

like image 567
Isak Savo Avatar asked Nov 12 '22 21:11

Isak Savo


1 Answers

Code shows the bottom right rectangle like in your "smaller tiles" visualisation:

var geometry = MyOriginalPath.Data.Clone();
var bounds = geometry.Bounds;    
var rectangleGeometry = new RectangleGeometry(bounds);
var halfWidth = bounds.Width * 0.5;
var halfHeight = bounds.Height * 0.5;
var bottomQuarter = new RectangleGeometry(
  new Rect(bounds.X + halfWidth, bounds.Y + halfHeight, 
           halfWidth, halfHeight));
var combinedGeometry = new CombinedGeometry(GeometryCombineMode.Exclude,
                                           rectangleGeometry, bottomQuarter);
combinedGeometry = new CombinedGeometry(GeometryCombineMode.Exclude,
                                        geometry, combinedGeometry);
MyBottomQuarterPath.Data = combinedGeometry;

Regards Dave

like image 154
David Hollinshead Avatar answered Nov 15 '22 12:11

David Hollinshead