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
 
 
 
 
 
TitleDraw text that is clipped to a graphics path in Visual Basic .NET
DescriptionThis example shows how to draw text that is clipped to a graphics path in Visual Basic .NET.
Keywordstext, path, clip
CategoriesVB.NET, Graphics
 
The form's Paint event handler makes a GraphicsPath object, adds an ellipse to it, and uses the form's SetClip method to make the form clip graphics to the path. It then draws a string repeatedly across the form's surface. Parts of the text that fall outside of the path are clipped.
 
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint
    e.Graphics.Clear(Me.BackColor)

    Dim graphics_path As New _
        System.Drawing.Drawing2D.GraphicsPath
    Dim path_rect As New RectangleF(10, 10, _
        Me.ClientSize.Width - 20, Me.ClientSize.Height - 20)
    graphics_path.AddEllipse(path_rect)
    e.Graphics.SetClip(graphics_path)

    Dim txt As String = "When in worry or in doubt run in " & _
        "circles scream and shout... "
    For i As Integer = 1 To 6
        txt &= txt
    Next i

    e.Graphics.TextRenderingHint = _
        System.Drawing.Text.TextRenderingHint.AntiAlias
    Dim rect As New RectangleF(0, 0, Me.ClientSize.Width, _
        Me.ClientSize.Height)
    e.Graphics.DrawString(txt, Me.Font, Brushes.Black, rect)
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated