gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:26:23 26 4
gpt4 key购买 nike

下面的代码画了一个十字:

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

enter image description here

我想这样画:

enter image description here

我试过使用 Flatten();CloseAllFigures(); 但它们不起作用。

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

enter image description here

这可以用 GraphicsPath 实现吗?

最佳答案

使用Regions是可以的,但如果没有其他解决方案,则应使用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();
}
}
}

关于c# - GraphicsPath 和 DrawPath - 删除相交线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28962998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com