FillPolygon with holes

2019-03-01 16:19发布

问题:

I want to create a "fill" that fills the inside of a polygon, created using a point list, but be able to remove its holes.

My old code:

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

It simply fills a polygon created by the contour of the points in the list.

How can I fill the polygon, but exclude the holes in it (which I know are inside, I have tested):

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

Is there something I can use, that has already been created ? Can I somehow draw the original polygon and remove the holes ? What would be the best approach ?

What have I tried - example: I have done the following:

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

This is not so bad... it does exclude the inner polygons, but it also draws a swath of "empty" between the contours. So, I guess it doesn't work.

Thank you.

Edit: Adding picture:

The contour is given as a list of points ("points"), the holes as a list of lists ("holes"). The picture on the right has a rough drawing of the swaths of lines that I get (even though the holes and the contours have no points in common) - the lines change as I move the image.

回答1:

Try using StartFigure and CloseFigure on your GraphicPath objects:

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

Without that, I think all of your objects are connecting to each other.