Fill Polygon
I am creating a polygon-like drawing. I am drawing an octogon. 7 sides have a pen color of red. The other has black. How can I fill the polygon or open-space with blue or green or even black?
You have an array of points you use to create the octagon, right? Take that array of points and pass it to the Graphics.FillPolygon method, passing in the appropriate brush to use, as well.
But, how can I take the array of points and split up the color of the pen? What if I wanted every other line to be a different color? Can you show me a sample in VB? Here is what I have:Dim myPoints() As Point = {New Point(10,30), New Point(100,10), New Point(150,75), New Point(100,150), New Point(10,130)}
myGraphics.DrawPolygon(Pens.Blue, myPoints)
I don't know of a "tricky" way to do it, but here's code that just worked for me:
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
Dim myPoints() As Point = {New Point(10, 30), New Point(100, 10), New Point(150, 75), New Point(100, 150), New Point(10, 130)}
e.Graphics.DrawPolygon(Pens.Blue, myPoints)
Dim i As Integer
Dim myPen As Pen
For i = 0 To myPoints.Length - 2
If (i Mod 2) = 0 Then
myPen = Pens.Red
Else
myPen = Pens.Blue
End If
e.Graphics.DrawLine(myPen, myPoints(i), myPoints(i + 1))
e.Graphics.FillPolygon(Brushes.Green, myPoints)
Next
End Sub
A few questions on this. When I do this, my graphics is not crisp. I get lines off of the edge of the image. Also, I would like to fill only half of the image. When I do FillPolygon, I can change the myPoints to be the adjusted points correct?