2011-08-31 83 views
1

如何用另一個剪輯System.Drawing.Drawing2D.GraphicsPath得到一個新的GraphicsPath?注意1:SetClip()可能是一個完整的System.Drawing.Graphics對象,但這裏需要的是某種相交的GraphicsPath來獲得另一個GraphicsPath。用另一個GraphicsPath剪切GraphicsPath

注2:這裏討論的方法(Intersecting GraphicsPath objects)返回一個區域。在這裏,我們期待的GraphicsPath

回答

1

我在這個快速去,和我最接近的是這樣的:

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

這樣做的問題是,GetRegionScans()爲您提供了數以千計的矩形的後面,而不僅僅是不同線的點。取消註釋DrawPath方法以查看我的意思。

有一個用於管理剪輯的開源庫,它似乎是相當活躍的here(通過this Stackoverflow question找到)。

我沒有經驗,但它看起來能夠做你以後的事情。

+0

我更喜歡外部庫。此演示代碼是解決方案: 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(); (GetClipType(),solution,GetPolyFillType(),GetPolyFillType()); –