Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
 
 
 
 
 
 
 
Old Pages
 
Old Index
Site Map
What's New
 
Books
How To
Tips & Tricks
Tutorials
Stories
Performance
Essays
Links
Q & A
New in VB6
Free Stuff
Pictures
 
 
 
TitleFill text with a LinearGradientBrush in VB .NET
Keywordsfilled text, lineargradientbrush, VB.NET
CategoriesGraphics, VB.NET
 
A LinearGradientBrush defines a color gradient that fades from one color to another between one point and another.

This example creates a GraphicsPath object and uses its AddString method to add a string to it. It uses the MeasureString method to figure out how big the resulting string will be. It then creates a LinearGradientBrush that fades from red to blue between the text's upper left corner and its lower right corner.

The code calls the Graphics object's FillPath method to fill the text with the gradient and then calls DrawPath to outline the text.

 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    Dim txt = " VB" & vbCrLf & ".NET"
    Dim font_size As Single = 64
    Dim text_path As New GraphicsPath()
    Dim font_family As New FontFamily("Comic Sans MS")

    ' Add text to the path.
    text_path.AddString(txt, _
        font_family, _
        System.Drawing.FontStyle.Bold, _
        font_size, _
        New PointF(5, 5), _
        System.Drawing.StringFormat.GenericDefault)

    ' Get the text's width and height.
    Dim txt_size As SizeF = _
        e.Graphics.MeasureString(txt, _
            New Font(font_family, _
                font_size, _
                FontStyle.Bold, _
                GraphicsUnit.Pixel))

    ' Fill the path with a gradient.
    e.Graphics.FillPath( _
        New System.Drawing.Drawing2D.LinearGradientBrush( _
            New Point(5, 5), _
            New Point(5 + txt_size.Width, 5 + _
                txt_size.Height), _
            Color.Red, _
            Color.Blue), _
        text_path)

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