绘制多边形与多个孔? [英] Drawing polygon with more than one hole?

查看:142
本文介绍了绘制多边形与多个孔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制多边形与多个孔。我尝试下面的代码,它不能正常工作。请指教

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.

推荐答案

使用的GraphicsPath 来代替。使用

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

这篇关于绘制多边形与多个孔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆