2011-01-31 71 views
1

我已经创建了一个简单的测试应用程序,根据我提供的要点将多边形绘制到图像上。我已经创建了一个画笔,它将填充多边形,我想如何。现在我想填写一切,但多边形。所以,使用我的画笔,我想绘制多边形的周围,所以可见的是多边形内的东西。有谁知道我可以怎样做到这一点?图形FillPolygon外观?

在此先感谢!

+0

假设这是C#/ .NET?可能要在这里编辑您的标签。 – MusiGenesis 2011-01-31 18:47:15

+0

对不起,我还不够具体。它实际上是VB.net,尽管我认为这些方法是相同的(功能性说法)。 – lhan 2011-01-31 20:42:02

回答

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的你绘制的程度,然后填补这个RECT,将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

+0

太棒了,我认为这对我有用!感谢您的快速回复! – lhan 2011-02-01 13:38:58

3

我很惊讶没有找到这个答案在任何地方,但在看文档System.Drawing.Region 答案看起来很简单。

我们可以从无限区域中排除多边形(我假设需要是GraphicsPath)。 Region.XOR应该工作一样在这种情况下,排除:

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

在我来说,我只是需要排除一个普通的RectangleF,但是这并获得成功,它填补了周围地区,并独自留在排除地区。

0

那些谁还没有找到解决方案,看看this。为我工作,开箱即用。