1

我创建了一个简单的测试应用程序,它将根据我提供的点在图像上绘制一个多边形。我创建了一个画笔,可以按照我想要的方式填充多边形。现在我想填写除多边形之外的所有内容。所以,使用我的画笔,我想在多边形周围绘制,所以所有可见的都是多边形内部的东西。有谁知道我如何能够做到这一点?

提前致谢!

4

3 回答 3

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 但这成功了,它填充了周围区域并单独留下了排除区域。

于 2011-03-17T17:46:05.827 回答
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

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

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

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

于 2011-01-31T21:05:31.243 回答
0

那些还没有找到解决方案的人,看看这个。为我工作,开箱即用。执行以下操作,

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

类似的输出将是这样的

于 2017-04-05T07:58:59.327 回答