简体   繁体   English

绘制有多个孔的多边形?

[英]Drawing polygon with more than one hole?

I'm trying to draw a polygon with more than one holes. 我正在尝试绘制一个有多个孔的多边形。 I tried the following code and it does not work correctly. 我尝试了以下代码,但它无法正常工作。 Please advise. 请指教。

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

The first part is the outer polygon. 第一部分是外部多边形。 The second and the third parts are the two holes inside the polygon. 第二和第三部分是多边形内的两个孔。

Use a GraphicsPath instead. 请改用GraphicsPath You can draw it with Graphics.FillPath , like this: 您可以使用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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM