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 centered on a picture
KeywordsPictureBox, text, center, picture
CategoriesGraphics
 
To center the text horizontally, use the PictureBox's TextWidth method to see how wide the text will be. Subtract that from the PictureBox's width and divide by two.
 
Private Sub Form_Load()
Const SAMPLE_TEXT As String = "Geodesic Sphere"

    ' Draw on the picture.
    picCanvas.Font.Name = "Times New Roman"
    picCanvas.Font.Size = 24
    picCanvas.Font.Bold = True
    picCanvas.ForeColor = vbBlack

    ' Center the text horizontally at the top
    ' of the picture.
    picCanvas.CurrentX = (picCanvas.ScaleWidth - _
        picCanvas.TextWidth(SAMPLE_TEXT)) / 2
    picCanvas.CurrentY = 0

    picCanvas.AutoRedraw = True
    picCanvas.Print SAMPLE_TEXT

    ' Make the text a permanent part of the image.
    ' This is important if you later need to copy
    ' the picture to another control or the Printer.
    picCanvas.Picture = picCanvas.Image
End Sub
 
You could perform a similar calculation using TextHeight and ScaleHeight to center the text vertically.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated