glassy
glassy

Reputation: 145

VB6 - Load/store images to be used later

I have an image control on my form and I want the picture to change based on certain events. Let's say there are four different possible images. I know that I can set the control to whatever image I want using:

imgBox1.Picture = LoadPicture(sPath & "img1.bmp")

But I guess my question really is, do I have to use the LoadPicture function every time I want to change imgBox1 to a different picture (say, "img2.bmp")? Or can I load the four different images to some kind of object and then just set imgBox1.Picture to equal that object? I've tried several different ways and can't get anything to work.

Upvotes: 3

Views: 828

Answers (2)

Alex K.
Alex K.

Reputation: 175766

StdPicture is the type to use to store an image.

The example below loads 3 images from disk once then cycles them on a button click.

Private mPics(2) As StdPicture
Private mIndex As Long

Private Sub Form_Load()
    Set mPics(0) = LoadPicture("C:\kitty_born.bmp")
    Set mPics(1) = LoadPicture("C:\kitty_life.bmp")
    Set mPics(2) = LoadPicture("C:\kitty_dead.bmp")
End Sub

Private Sub someButton_Click()
    If mIndex > UBound(mPics) Then mIndex = 0

    Set somePictureOrImageBox.Picture = mPics(mIndex)

    mIndex = (mIndex + 1)
End Sub

Upvotes: 3

CMaster
CMaster

Reputation: 389

You could create a control array of Image/PictureBox controls, each holding one of the several images, and set their visibility depending on the image you want displayed at a time.

Alternativley, you could have a hidden control array of PictureBoxs and then use the PaintPicture method of the PictureBox you wish to change to paste in the desired image.

Upvotes: 0

Related Questions