图形FillPolygon外观?

时间:2011-01-31 18:32:44

标签: vb.net graphics drawing polygon fill

我已经创建了一个简单的测试应用程序,它会根据我提供的点在图像上绘制多边形。我已经创建了一个画笔,它将填充多边形我想要的方式。现在我想要填充多边形的一切。因此,使用我的画笔,我想在多边形周围绘制所以可见的是多边形内部的内容。有谁知道我怎么能够做到这一点?

提前致谢!

3 个答案:

答案 0 :(得分:3)

我很惊讶没有找到这个答案,只是查看System.Drawing.Region的文档。  答案似乎很简单。

我们可以从无限区域中排除多边形(我假设它需要是一个GraphicsPath)。在这种情况下,Region.XOR应与Exclude相同:

            Region region = new Region();
            region.MakeInfinite();
            GraphicsPath polygonPath = GetYourPolygon();
            region.Exclude(polygonPath);
            e.Graphics.FillRegion(Brushes.Black, region);

在我的情况下,我只需要排除一个普通的RectangleF但是这样做了,它填充了周围区域并且单独留下了被排除的区域。

答案 1 :(得分:1)

我认为System.Drawing.Graphics.Clip就是你想要的。

以下是该链接的代码示例:

Private Sub SetAndFillClip(ByVal e As PaintEventArgs)

    ' Set the Clip property to a new region.
    e.Graphics.Clip = New Region(New Rectangle(10, 10, 100, 200))

    ' Fill the region.
    e.Graphics.FillRegion(Brushes.LightSalmon, e.Graphics.Clip)

    ' Demonstrate the clip region by drawing a string
    ' at the outer edge of the region.
    e.Graphics.DrawString("Outside of Clip", _
        New Font("Arial", 12.0F, FontStyle.Regular), _
        Brushes.Black, 0.0F, 0.0F)

End Sub

要填充区域之外的所有内容,那么在将Graphics.Clip设置为从您的点创建的区域之后,您必须确定要绘制的DC的范围,然后填充该矩形。

因此,您的代码可能如下所示:

Private Sub SetAndFillClip(ByVal e As PaintEventArgs)

    ' Set the Clip property to a new region.
    e.Graphics.Clip = GetRegionFromYourPoints()

    ' Fill the entire client area, clipping to the Clip region
    e.Graphics.FillRectangle(Brushes.LightSalmon, GetWindowExtentsFromYourWindow())
End Sub

此链接显示如何从点阵列创建区域:

http://www.vb-helper.com/howto_net_control_region.html

答案 2 :(得分:0)

那些尚未找到解决方案的人,请查看this。为我工作,开箱即用。请执行以下操作,

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    var points = new []
    {              
        new PointF(150, 250),
        new PointF( 50, 500),
        new PointF(250, 400),
        new PointF(300, 100),
        new PointF(500, 500),
        new PointF(500,  50),
    };

    using (var path = new GraphicsPath())
    {
        path.AddPolygon(points);

        // Uncomment this to invert:
        // p.AddRectangle(this.ClientRectangle);

        using (var brush = new SolidBrush(Color.Black))
        {
            e.Graphics.FillPath(brush, path);
        }
    }
}

A similar output will be like this

相关问题