【问题标题】:Drawing polygon with more than one hole?绘制具有多个孔的多边形?
【发布时间】:2023-03-30 15:42:01
【问题描述】:

我正在尝试绘制一个具有多个孔的多边形。我尝试了以下代码,但它无法正常工作。请指教。

    PointF[] mypoly = new PointF[6 + 5 + 5];

    mypoly[0] = new PointF(0, 0);
    mypoly[1] = new PointF(100, 0);
    mypoly[2] = new PointF(100, 100);
    mypoly[3] = new PointF(0, 100);
    mypoly[4] = new PointF(10, 80);
    mypoly[5] = new PointF(0, 0);

    mypoly[6] = new PointF(10, 10);
    mypoly[7] = new PointF(10, 20);
    mypoly[8] = new PointF(20, 20);
    mypoly[9] = new PointF(20, 10);
    mypoly[10] = new PointF(10, 10);

    mypoly[11] = new PointF(40, 10);
    mypoly[12] = new PointF(40, 20);
    mypoly[13] = new PointF(60, 20);
    mypoly[14] = new PointF(60, 10);
    mypoly[15] = new PointF(40, 10);

    g.FillPolygon(new SolidBrush(Color.Red), mypoly, FillMode.Winding);

第一部分是外多边形。第二和第三部分是多边形内部的两个孔。

【问题讨论】:

  • 你得到了什么?有什么理由不能是 3 个单独的多边形吗?
  • FillMode 有什么尝试?这控制了交叉点的渲染方式。
  • 为什么不用一种颜色绘制外部多边形,用“背景”颜色绘制内部多边形?

标签: c# c++ gdi+ gdi


【解决方案1】:

请改用GraphicsPath。你可以用Graphics.FillPath画出来,像这样:

using System.Drawing.Drawing2D;
...
    using (var gp = new GraphicsPath()) {
        PointF[] outer = new PointF[] { new PointF(0, 0), new PointF(100, 0), 
            new PointF(100, 100), new PointF(0, 100), new PointF(10, 80),new PointF(0, 0) };
        gp.AddPolygon(outer);  
        PointF[] inner1 = new PointF[] { new PointF(10, 10), new PointF(10, 20), 
            new PointF(20, 20), new PointF(20, 10), new PointF(10, 10) };
        gp.AddPolygon(inner1);
        PointF[] inner2 = new PointF[] { new PointF(40, 10), new PointF(40, 20), 
            new PointF(60, 20), new PointF(60, 10), new PointF(40, 10) };
        gp.AddPolygon(inner2);
        e.Graphics.FillPath(Brushes.Black, gp);
    }

【讨论】:

  • 在阅读您的答案之前,我已经找到了解决方案。它就在 Microsoft 网站的 FillPath 参考页面上。他们的答案和你的一样。
猜你喜欢
  • 1970-01-01
  • 2017-10-23
  • 2016-10-06
  • 2019-07-26
  • 2016-05-07
  • 2017-12-08
  • 1970-01-01
  • 2017-12-19
  • 2018-04-17
相关资源
最近更新 更多