1

How do you Clip a System.Drawing.Drawing2D.GraphicsPath with another GraphicsPath to get a new GraphicsPath?

Note 1: It is possible to SetClip() a whole System.Drawing.Graphics object, but what is needed here is some sort of Intersecting a GraphicsPath to get another GraphicsPath.

Note 2: The method discussed here (Intersecting GraphicsPath objects) returns a region. Here we expect a GraphicsPath

Community
  • 1
  • 1
el_shayan
  • 2,735
  • 4
  • 28
  • 42

1 Answers1

1

I had a quick go at this, and the closest I got was this:

var region = new Region(gp1);
region.Intersect(gp2);

var gpResult = new GraphicsPath();
gpResult.AddRectangles(region.GetRegionScans(new Matrix()));
gpResult.CloseAllFigures();

using (var br = new SolidBrush(Color.LightYellow))
{
    e.Graphics.FillPath(br, gpResult);
    //e.Graphics.DrawPath(Pens.Black, gpResult);
}

The problem with this is that GetRegionScans() gives you thousands of rectangles back, not just the distinct points of the lines. Uncomment the DrawPath method to see what I mean.

There is an open source library for manageing clipping, which seems to be reasonably active here (Found via this Stackoverflow question).

I have no experience with it, but it looks to be able to do what you are after.

Community
  • 1
  • 1
Pondidum
  • 11,457
  • 8
  • 50
  • 69
  • I prefer the external library. This code from its demo is the solution: Polygons solution2 = new Polygons(); Clipper c = new Clipper(); c.UseFullCoordinateRange = false; c.AddPolygons(subjects, PolyType.ptSubject); c.AddPolygons(clips, PolyType.ptClip); exSolution.Clear(); solution.Clear(); c.Execute(GetClipType(), solution, GetPolyFillType(), GetPolyFillType()); – el_shayan Aug 31 '11 at 12:48