删除矩形之间共享的线段

时间:2011-12-05 23:36:24

标签: c# graphics

第一张图片是我所拥有的 - Rectangle的列表,我用它来“突出显示”一串字符。第二张图片(右图)是我想看到的模型。

删除共享线段的最佳方法是什么?

What I have What I want

1 个答案:

答案 0 :(得分:0)

好的,我想我找到了一个不错的解决方案。

// create a pen with a width of 2px (half of it will be blanked out so it will
// essentially be a width of 1px)
Pen pen = new Pen(Color.Blue, 2f);

// build a GraphicsPath with the rectangles
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(...);
gp.AddRectangle(...);
gp.AddRectangle(...);

// g is our Graphics object
// exclude the region so the path doesn't draw over it
Region reg = new Region(gp);
g.ExcludeClip(reg);

// now draw path, it won't show up inside the excluded clipping region
g.DrawPath(pen, gp);

// clean up
g.ResetClip();
pen.Dispose();
gp.Dispose();
reg.Dispose();