c# – GraphicsPath和DrawPath – 删除相交线

以下代码绘制了一个十字架:

using (SolidBrush brush = new SolidBrush(Color.FromArgb(192, 99, 104, 113)))
{
    using(GraphicsPath path = new GraphicsPath())
    {
        path.AddRectangle(new Rectangle(e.ClipRectangle.X + (e.ClipRectangle.Width - 40) / 2, e.ClipRectangle.Y, 40, e.ClipRectangle.Height));
        path.AddRectangle(new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + (e.ClipRectangle.Height - 40) / 2, e.ClipRectangle.Width, 40));
        path.FillMode = FillMode.Winding;
        e.Graphics.DrawPath(Pens.DimGray, path);
    }
}

我想像这样绘制它:

我尝试过使用Flatten();和CloseAllFigures();但这些都行不通.

我正在寻找像Union这样的效果:

这对GraphicsPath有可能吗?

最佳答案 可以使用Regions.But如果没有其他解决方案,您应该使用GDI中的API FrameRgn来绘制区域的框架.

        Graphics g = e.Graphics;
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(192, 99, 104, 113)))
        {
            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddRectangle(new Rectangle(e.ClipRectangle.X + (e.ClipRectangle.Width - 40) / 2, e.ClipRectangle.Y, 40, e.ClipRectangle.Height));
                path.AddRectangle(new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + (e.ClipRectangle.Height - 40) / 2, e.ClipRectangle.Width, 40));
                path.FillMode = FillMode.Winding;
                using (Region region = new Region(path))
                {
                    IntPtr reg = region.GetHrgn(g);
                    IntPtr hdc = g.GetHdc();
                    IntPtr brushPtr = Win32.GetStockObject(Win32.WHITE_BRUSH);
                    IntPtr oldbrushPtr = Win32.SelectObject(hdc, brushPtr);
                    Win32.FrameRgn(hdc, reg, brushPtr, 1, 1);
                    Win32.DeleteObject(brushPtr);
                    Win32.SelectObject(hdc, oldbrushPtr);
                    region.ReleaseHrgn(reg);
                    g.ReleaseHdc();
                }
            }
        }
点赞