首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GraphicsPath和DrawPath -去除相交线

GraphicsPath和DrawPath -去除相交线
EN

Stack Overflow用户
提问于 2015-03-10 11:50:41
回答 4查看 2.8K关注 0票数 4

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

代码语言:javascript
复制
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();,但是这些都不起作用。

我在找一个像联盟这样的效果:

这与GraphicsPath是可能的吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-04-11 01:39:31

使用Regions.But是可能的,如果没有其他解决方案,您应该使用GDI中的API FrameRgn绘制该区域的框架。

代码语言:javascript
复制
        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();
                }
            }
        }
票数 1
EN

Stack Overflow用户

发布于 2015-03-10 12:13:13

我知道这不是一个完美的解决方案,但要把它当作一种选择:

代码语言:javascript
复制
        using (SolidBrush brush = new SolidBrush(Color.FromArgb(192, 99, 104, 113)))
        {
            using (GraphicsPath path = new GraphicsPath())
            {
                var rect1 = new Rectangle(e.ClipRectangle.X + (e.ClipRectangle.Width - 40) / 2, e.ClipRectangle.Y, 40, e.ClipRectangle.Height - 1);
                var rect2 = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y + (e.ClipRectangle.Height - 40) / 2, e.ClipRectangle.Width - 1, 40);
                path.AddRectangle(rect1);
                path.AddRectangle(rect2);
                e.Graphics.DrawPath(Pens.DimGray, path);

                var bgRect1 = new Rectangle(rect1.X + 1, rect1.Y + 1, rect1.Width - 1, rect1.Height - 1);
                var bgRect2 = new Rectangle(rect2.X + 1, rect2.Y + 1, rect2.Width - 1, rect2.Height - 1);
                using (GraphicsPath backgroundPath = new GraphicsPath())
                {
                    backgroundPath.AddRectangle(bgRect1);
                    backgroundPath.AddRectangle(bgRect2);
                    e.Graphics.FillPath(Brushes.White, backgroundPath);
                }
            }
        }

其结果是:

票数 1
EN

Stack Overflow用户

发布于 2015-03-10 12:09:54

这是可能的,你想要的是regions。看一看:

GDI+中的区域

我猜你下一步应该是勾勒出这个地区。这是困难的部分,因为没有API可以这样做。最简单的办法是:

  • 更厚的笔画出你在第一张照片中所做的一切。
  • 在路径之外创建区域,并合并产生的区域。
  • 填充或删除生成的联合区域(有API )。

这将产生你附上的第二张图片。区域操作需要一些创造性,因为缺乏边缘检测、轮廓或区域对路径的API来帮助您绘制结果。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28962998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档