Vb.net 图形填充多边形外部?

Vb.net 图形填充多边形外部?,vb.net,graphics,drawing,polygon,fill,Vb.net,Graphics,Drawing,Polygon,Fill,我已经创建了一个简单的测试应用程序,可以根据我提供的点在图像上绘制多边形。我已经创建了一个笔刷,可以按照我想要的方式填充多边形。现在我想填充除了多边形以外的所有东西。所以,我想用画笔在多边形周围画画,这样所有可见的都是多边形内部的东西。有人知道我怎样才能做到这一点吗 提前谢谢 我想这就是你想要的 以下是该链接的代码示例: Private Sub SetAndFillClip(ByVal e As PaintEventArgs) ' Set the Clip property to a

我已经创建了一个简单的测试应用程序,可以根据我提供的点在图像上绘制多边形。我已经创建了一个笔刷,可以按照我想要的方式填充多边形。现在我想填充除了多边形以外的所有东西。所以,我想用画笔在多边形周围画画,这样所有可见的都是多边形内部的东西。有人知道我怎样才能做到这一点吗

提前谢谢

我想这就是你想要的

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

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
要填充区域外的所有内容,则必须确定要绘制的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
此链接显示如何从点阵列创建区域:

我想这就是你想要的

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

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
要填充区域外的所有内容,则必须确定要绘制的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
此链接显示如何从点阵列创建区域:


我很惊讶,除了在查看相关文档之外,在任何地方都没有找到这个答案 答案似乎很简单

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

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

在我的例子中,我只需要排除一个普通的矩形,但这起作用了,它填满了周围的区域,只留下了被排除的区域。

我惊讶地发现,除了查看文档中的 答案似乎很简单

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

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

在我的例子中,我只需要排除一个普通的矩形,但这样做了,它填充了周围的区域,并留下了被排除的区域。

那些还没有找到解决方案的人,看看。为我工作,开箱即用。做如下的事情

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

那些尚未找到解决方案的人,请查看。为我工作,开箱即用。做如下的事情

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

假设这是C#/.NET?可能想在这里编辑你的标签。对不起,我不够具体。它实际上是VB.net,尽管我认为方法是相同的(从功能上讲)。假设这是C#/.net?可能想在这里编辑你的标签。对不起,我不够具体。它实际上是VB.net,尽管我认为方法是相同的(从功能上讲)。太棒了,我认为这对我来说是可行的!谢谢你的快速回复!太棒了,我想这对我会有用的!谢谢你的快速回复!顺便说一句,我认为瓦格戈的答案行不通。我认为将“剪辑”设置为多边形区域会将绘图限制为仅在该区域内。如果我们在将Clip设置为多边形后尝试填充窗口,我们将只填充多边形。顺便说一句,我认为wageoghe的答案行不通。我认为将“剪辑”设置为多边形区域会将绘图限制为仅在该区域内。如果我们在将Clip设置为多边形后尝试填充窗口,我们将只填充多边形。虽然此链接可能会回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能无效。-是的,我现在添加了更多信息。谢谢你的建议。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-是的,我现在添加了更多信息。谢谢你的建议。