PictureBox Control

Displaying Images

If you know what image you want to display during design time, you can simply click the ... next to the picture property, and select the image you want to display.

To load an image at run-time, you can use the Picture property, and the LoadPicture method:

Set PictureBox.Picture = LoadPicture(strFilePath)

Where PictureBox is the name of your PictureBox control, and strFilePath is the path of the image you want displayed.

The following formats are supported for loading:

Bitmaps (*.bmp;*.dib)
GIF Images (*.gif)
JPEG Images (*.jpg)
MetaFiles (*.wmf;*.emf)
Icons (*.ico;*.cur)

If you want to display an animated GIF file, then you can use this dll.

If you want the picture box to automatically resize to display the whole of the picture, then you can set its AutoSize property to True. 

However, if the image will not fit onto the screen, or you do not want to take up the whole screen, you will need to add some scroll bars. This is more complicated than it should be. I think the best way is to use the VB Accelerator Flat Scroll bar control. Once you have downloaded it, create a new project. Then add the Flat Scroll Bar control, and name it scrPort. Select the control, and then add a Image control (we use the Image control, because  we do not need the PictureBox's features. You could use one if you wanted to), and call that imgPic. Then add the following code:


Private Sub Form_Load()
    '// set the path of the image
    sPic = "D: oolbox.gif"
    '// display the picture
    Set imgPic.Picture = LoadPicture(sPic)
    With scrPort
        '// set the scroll bar properties
        .ActiveBar = efstHorizontal 
        .Max = (imgPic.Width - scrPort.ScaleWidth) Screen.TwipsPerPixelX
        .LargeChange = scrPort.ScaleWidth (Screen.TwipsPerPixelX * 2)
        .SmallChange = 8
    
        .ActiveBar = efstVertical
        .Max = (imgPic.Height - scrPort.ScaleHeight) Screen.TwipsPerPixelY
        .LargeChange = scrPort.ScaleHeight (Screen.TwipsPerPixelY * 2)
        .SmallChange = 8
    End With
    '// Move the image to the edge of the control
    imgPic.Move 0, 0
End Sub

Private Sub scrPort_Change()
    Dim lL As Long
    Dim lT As Long

    With scrPort
        .ActiveBar = efstHorizontal
        lL = -1 * .Value * Screen.TwipsPerPixelX
        .ActiveBar = efstVertical
        lT = -1 * .Value * Screen.TwipsPerPixelY
    End With
    '// move the image
    With imgPic
        .Move lL, lT, .Width, .Height
    End With
End Sub

Private Sub scrPort_Scroll()
    scrPort_Change
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov