Wednesday, April 1, 2009

fill the area outside of a polygon with a color

The graphics object has a fillpolygon function.  I wanted a solution to the problem of receiving an image and a polygon points and the need to fill the area outside of that polygon with a color.  The solution below solves that problem.

Imports System.Drawing.Drawing2D
Public Class Form1

  Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    Dim polypoints() As Point

    ' Draw a polygon.
    polypoints = New Point() { _
        New Point(25, 100), _
        New Point(275, 350), _
        New Point(275, 100), _
        New Point(25, 350) _

    Using gp As New GraphicsPath
      Using rgn As New Region(gp)
        e.Graphics.FillRectangle(Brushes.Red, Me.ClientRectangle)
      End Using
    End Using

  End Sub

End Class

No comments: