【问题标题】:How to Fill a path excluding certain areas(paths) C# Graphics如何填充不包括某些区域(路径)的路径 C# 图形
【发布时间】:2019-02-09 19:35:55
【问题描述】:

现在我正在开发一个 Windows 窗体 C# 应用程序,它使用 dlib (https://github.com/takuya-takeuchi/DlibDotNet) 检测你的脸
它得到了 68 个带有 Points 的人脸地标

面部地标及其点:

因此,我为嘴唇、鼻子、眼睛和眉毛制作了一个图形路径,以及一条环绕整个脸部的路径,我的问题是,是否可以减去眼睛、眉毛、嘴唇和鼻子的路径从覆盖整个面部到绘制所有面部“排除”这些区域?

我发现这在 xaml 中是可能的:

(https://docs.microsoft.com/en-us/visualstudio/designers/draw-shapes-and-paths?view=vs-2017)

那么是否可以在 C# 中使用图形路径和位图来执行 ExcludeOverlap 或 Substract? 如果是的话怎么办?

(我知道发布一些代码几乎是一个潜规则,但我所做的基本上是为面部的每个部分创建一个图形路径,然后使用 Graphics.FillPath() 将它们绘制在位图上)

【问题讨论】:

  • 运气好并经过测试,它会自行发生。您需要尝试为路径添加形状、关闭图形并找到正确的缠绕模式。
  • 由于我不知道什么是windingmode,所以我猜想可能有答案,我去看看。
  • 它是一个 GraphicsPath 属性,用于控制如何填充重叠区域。
  • 嗯,可能是这样,谢谢指出
  • 嗯,我总是记不住正确的名字:它是GraphicsPath.FillMode - 没有重叠你不需要更改任何东西,只需创建一些路径并将它们添加到带有@的路径987654328@ 重叠时,您可能希望将默认 Alternate 更改为 Winding..

标签: c# winforms graphics dlib


【解决方案1】:

是否可以从覆盖整个面部的路径中减去眼睛、眉毛、嘴唇和鼻子的路径,以绘制“排除”这些区域的所有面部?

这不仅是可能的;事实上,这是组合 GraphicsPaths 的默认设置:您将较小的内部路径添加到较大的外部路径,当您填充它时,它们将成为孔。

但是,当您在“孔”上覆盖更多路径时,这也不会发生,从而导致孔内的正区域。

要使所有路径相加组合(Or -ing),您可以将FillMode 属性更改为Winding。默认值为“Alternative”,这将创建孔(Xor -ing the araes。)

要获得完全控制,您可以使用Regions。它们可以与整套集合操作随意组合。但它们不支持抗锯齿,因此曲线和倾斜的线条会显得粗糙。

例子:

private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp0 = new GraphicsPath();
    GraphicsPath gp1 = new GraphicsPath();
    GraphicsPath gp2 = new GraphicsPath();
    GraphicsPath gp3 = new GraphicsPath();
    GraphicsPath gp4 = new GraphicsPath();

    gp0.AddEllipse(11, 11, 333, 333);
    gp1.AddEllipse(55, 55, 55, 55);
    gp2.AddEllipse(222, 55, 66, 66);
    gp3.AddEllipse(55, 222, 99, 222);
    gp4.AddLine(66, 123, 234, 77);

    using (Pen pen = new Pen(Color.Empty, 12f))
    gp4.Widen(pen);

    gp0.AddPath(gp1, true);
    gp0.AddPath(gp2, true);
    gp0.AddPath(gp3, true);
    gp0.AddPath(gp4, true);

    gp0.FillMode = FillMode.Alternate;
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
    e.Graphics.FillPath(Brushes.Goldenrod, gp0);
}

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-28
    相关资源
    最近更新 更多