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
 
 
 
 
 
TitleCenter an image on a form
Keywordscenter, image, form
CategoriesTips and Tricks
 
Copy the image into a hidden PictureBox. Then use PaintPicture to center it on the form.
 
Private Sub cmdLoad_Click()
    On Error GoTo LoadError

    ' Load the picture into picHidden.
    picHidden.Picture = LoadPicture(txtFileName.Text)

    ' Center the picture.
    Form_Resize
    Exit Sub
    
LoadError:
    MsgBox "Error" & Str$(Err.Number) & _
        " loading picture." & vbCrLf & _
        Err.Description
End Sub

Private Sub Form_Load()
    picHidden.AutoSize = True
    picHidden.BorderStyle = vbBSNone
    
    txtFileName = App.Path & "\"
End Sub

' Center the image.
Private Sub Form_Resize()
Dim to_x As Single
Dim to_y As Single

    If picHidden.Picture = 0 Then Exit Sub
    
    Cls
    
    ' See where we need to put the picture to center it.
    to_x = (ScaleWidth - picHidden.ScaleWidth) / 2
    to_y = (ScaleHeight - picHidden.ScaleHeight) / 2

    ' Copy the picture centered on the form.
    Me.PaintPicture picHidden.Picture, _
        to_x, to_y
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated