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 an area with a PathGradientBrush in VB .NET
KeywordsPathGradientBrush, VB.NET
CategoriesGraphics, VB.NET
 
A PathGradientBrush is defined by a path. It fills the area inside the path by shading color from a center point to the edges of the path. For example, imagine an ellipse that shades from white to red from the center to the edge.

This example creates a Point array to define the path. It creates a GraphicsPath object and uses its AddClosedCurve method to add a closed curve to the path.

Next the program defines a PathGradientBrush. The SurroundColors property is an array defining the colors the brush should use for each point on the path. This is troublesome for a closed curve because the points on the path are defined by the spline not directly by the program. This example uses the single color blue for the entire path.

The code sets the brush's center color to green, fills the path, and then outlines the path.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    Dim wid As Integer = Me.ClientSize.Width
    Dim hgt As Integer = Me.ClientSize.Height
    Dim pts() As Point = { _
        New Point(wid * 0.1, hgt * 0.3), _
        New Point(wid * 0.3, hgt * 0.3), _
        New Point(wid * 0.3, hgt * 0.1), _
        New Point(wid * 0.7, hgt * 0.1), _
        New Point(wid * 0.7, hgt * 0.3), _
        New Point(wid * 0.9, hgt * 0.3), _
        New Point(wid * 0.9, hgt * 0.7), _
        New Point(wid * 0.7, hgt * 0.7), _
        New Point(wid * 0.7, hgt * 0.9), _
        New Point(wid * 0.3, hgt * 0.9), _
        New Point(wid * 0.3, hgt * 0.7), _
        New Point(wid * 0.1, hgt * 0.7) _
    }
    Dim curve_path As New GraphicsPath()
    curve_path.AddClosedCurve(pts, 0.5)

    ' Define the brush.
    Dim curve_brush As New _
        System.Drawing.Drawing2D.PathGradientBrush(curve_path)
    Dim surround_colors() As Color = {Color.Blue}
    curve_brush.SurroundColors = surround_colors
    curve_brush.CenterColor = Color.Green

    ' Fill the shape.
    e.Graphics.FillPath(curve_brush, curve_path)

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