Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Fill the area outside a Polygon (opposite of Graphics.FillPolygon)

Posted on 2009-03-31
4
Medium Priority
?
917 Views
Last Modified: 2013-12-17
The code snippet below will draw a polygon and then fill the area inside of that polygon.  I would like to draw a polygon (an array of points) and fill the area OUTSIDE of that polygon with a color - basically blank the area outside of a polygon with a color

Thanks
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(20, 110), _
        New Point(90, 150), _
        New Point(90, 100), _
        New Point(20, 150) _
    }
    e.Graphics.FillPolygon(Brushes.Red, polypoints)
    e.Graphics.DrawPolygon(Pens.Green, polypoints)
  End Sub

Open in new window

0
Comment
Question by:tedunni
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:jjardine
ID: 24033807
Could you not just draw a box or rectangle over the entire screen and fill it with your color, then draw the polygon and fill it as white?
0
 

Author Comment

by:tedunni
ID: 24033891
What I receive first is a bitmap image with colors and shapes already on it.  I also receive the polygon as an input.  I then take that bitmap and then draw a polygon on it.  At that point I want to color the area outside of the polygon.  

I don't believe I can do what you are suggesting jjardine because I would lose the image data that is inside the polygon.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 24034376
Here is a simple example...
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) _
        }
 
        Dim gp As New GraphicsPath
        gp.AddPolygon(polypoints)
        Dim rgn As New Region(gp)
        e.Graphics.ExcludeClip(rgn)
        e.Graphics.FillRectangle(Brushes.Red, Me.ClientRectangle)
    End Sub
 
End Class

Open in new window

FillOutsidePolygon.jpg
0
 

Author Closing Comment

by:tedunni
ID: 31565067
Thanks, another perfect solution.
0

Featured Post

Automating Terraform w Jenkins & AWS CodeCommit

How to configure Jenkins and CodeCommit to allow users to easily create and destroy infrastructure using Terraform code.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question