|
|
Title | Draw text centered on a picture |
Keywords | PictureBox, text, center, picture |
Categories | Graphics |
|
|
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.
|
|
|
|
|
|