Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFill a polygon with a PathGradientBrush in Visual Basic .NET
DescriptionThis example shows how to fill a polygon with a PathGradientBrush in Visual Basic .NET.
Keywordspath gradient, gradient, PathGradientBrush, fill, draw, VB.NET
CategoriesVB.NET, Graphics
 
The program allocates an array to hold points that define a hexagon. It loops through the array creating points offset from the form's center. The points lie on an ellipse that would touch the form's four edges. The angle from the center to the first point is 0 radians (to the right). Each point after that is at the angle 2 * Pi / 6 radians larger than the previous angle (where m_N is the number of points).

Next the program uses the path to create a PathGradientBrush. It sets the brush's center color to white. It sets the brush's sourround colors to an array of colors, one per point defining the hexagon. It then uses the brush to fill the polygon.

 
' Draw a filled hexagon.
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Make the points for a hexagon.
    Dim pts(5) As Point
    Dim cx As Integer = Me.ClientSize.Width \ 2
    Dim cy As Integer = Me.ClientSize.Height \ 2
    Dim theta As Double = 0
    Dim dtheta As Double = 2 * Math.PI / 6
    For i As Integer = 0 To pts.Length - 1
        pts(i).X = CInt(cx + cx * Cos(theta))
        pts(i).Y = CInt(cy + cy * Sin(theta))
        theta += dtheta
    Next i

    ' Make a path gradient brush.
    Dim path_brush As New PathGradientBrush(pts)
    path_brush.CenterColor = Color.White
    Dim edge_colors() As Color = { _
        Color.Red, Color.Yellow, Color.Lime, _
        Color.Cyan, Color.Blue, Color.Magenta _
    }
    path_brush.SurroundColors = edge_colors

    ' Fill the hexagon.
    e.Graphics.FillPolygon(path_brush, pts)
    path_brush.Dispose()

    ' Outline the hexagon.
    e.Graphics.DrawPolygon(Pens.Black, pts)
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated