FillPolygon带孔

时间:2013-02-25 21:48:35

标签: vb.net graphics drawing polygon

我想创建一个填充多边形内部的“填充”,使用点列表创建,但能够删除它的孔。

我的旧代码:

Private Sub DrawSomething(ByVal points as List(of Point), _
ByVal myBrush As System.Drawing.Brush, _
ByVal myGraphics As System.Drawing.Graphics)
  myGraphics.FillPolygon(myBrush, points)
End Sub

它只是填充由列表中点的轮廓创建的多边形。

如何填充多边形,但排除其中的孔(我知道它在里面,我已经测试过):

Private Sub DrawSomething(ByVal points as List(of Point), _
ByVal holes as List(of List(of Point)), _ 
ByVal myBrush As System.Drawing.Brush, _
ByVal myGraphics As System.Drawing.Graphics)

' fill the contour created by points, excluding the contours created by holes
End Sub

有什么我可以使用的,已经创建了吗?我可以以某种方式绘制原始多边形并删除孔吗?什么是最好的方法?

我尝试了什么 - 示例:我已完成以下操作:

Private Sub DrawSomething(ByVal points as List(of Point), _
ByVal holes as List(of List(of Point)), _ 
ByVal myBrush As System.Drawing.Brush, _
ByVal myGraphics As System.Drawing.Graphics)

  Dim myGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)
  myGraphicsPath.AddLines(points)
  Dim myRegion As System.Drawing.Region = New System.Drawing.Region(myGraphicsPath)
  Dim otherGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath(Drawing2D.FillMode.Winding)  
  ForEach otherPoints as List(of Point) in holes
    otherGraphicsPath.AddLines(otherPoints)
  Next
  myRegion.Exclude(otherGraphicsPath)
  myGraphics.FillRegion(myBrush, myRegion)

End Sub

这不是那么糟糕......它确实排除了内部多边形,但它也在轮廓之间画了一条“空”。所以,我想它不起作用。

谢谢。

修改:添加图片:enter image description here

轮廓作为点列表(“点”)给出,孔作为列表列表(“孔”)。右边的图片粗略绘制了我得到的条纹(即使孔和轮廓没有共同的点) - 线条随着我移动图像而改变。

1 个答案:

答案 0 :(得分:1)

尝试在GraphicPath对象上使用StartFigure和CloseFigure:

For Each otherPoints as List(of Point) in holes
  otherGraphicsPath.StartFigure()
  otherGraphicsPath.AddLines(otherPoints)
  otherGraphicsPath.CloseFigure()
Next

没有它,我认为你的所有物体都相互连接。