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
 
 
 
 
 
 
TitleMake a LinearGradientBrush that blends three colors in VB .NET
DescriptionThis example shows how to make a LinearGradientBrush that blends three colors in VB .NET. This example blends from red to green to blue.
KeywordsLinearGradientBrush, ColorBlend, color, drawing, blend
CategoriesVB.NET, Graphics
 
The program makes a LinearGradientBrush.

It then makes a ColorBlend object. It sets the object's Colors property to an array listing the colors that it should blend. It sets the Positions property to an array of Singles between 0 and 1 giving the positions of the colors in the Colors array within the blend.

In this example, the colors are {red, green, blue} and the positions are {0.0, 0.5, 1.0} so the blend starts out red at position 0.0, blends to green at position 0.5 (halfway through the blend), and ends with blue at position 1.0 (the end).

The program sets the Brush's InterpolationColors property to the ColorBlend object and then fills two rectangles with it, with and without gamma correction.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    ' Make the basic brush.
    Dim x1 As Integer = 10
    Dim x2 As Integer = Me.ClientSize.Width - 10
    Dim the_brush As LinearGradientBrush
    the_brush = New LinearGradientBrush( _
        New Point(x1, 0), New Point(x2, 0), _
        Color.Red, Color.Blue)
    the_brush.WrapMode = WrapMode.TileFlipX

    ' Define the colors.
    Dim color_blend As New ColorBlend
    color_blend.Colors = New Color() {Color.Red, _
        Color.Green, Color.Blue}
    color_blend.Positions = New Single() {0.0, 0.5, 1.0}
    the_brush.InterpolationColors = color_blend

    ' Fill a rectangle without gamma correction.
    e.Graphics.DrawString("Without Gamma Correction", _
        Me.Font, Brushes.Black, x1, 10)
    e.Graphics.FillRectangle(the_brush, x1, 30, x2 - x1, 40)

    ' Fill a rectangle with gamma correction.
    the_brush.GammaCorrection = True
    e.Graphics.FillRectangle(the_brush, x1, 80, x2 - x1, 40)
    e.Graphics.DrawString("With Gamma Correction", _
        Me.Font, Brushes.Black, x1, 125)
End Sub
 
My next book includes a lot more information on Pens, Brushes, and other drawing objects in VS 2005. Check the VB Helper Web site or sign up for my newsletter to learn more about this book when it is available.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated