Link to home
Start Free TrialLog in
Avatar of cachedVB
cachedVB

asked on

500 POINTS (URGENT but not hard): Picturebox

How do I load an image from a file into a picturebox, given the image name (something.jpg).  Also, how can I pick 3 random files which were NOT the image loaded before (so now I have 4 unique pictures).  They will all be in the same folder, which should make it easy.
ASKER CERTIFIED SOLUTION
Avatar of vb_elmar
vb_elmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cachedVB
cachedVB

ASKER

Ok thanks for the quick answer, i just need to check if it works.  You should either get a response or 500 points soon :).
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

First read images from a folder to an array or collection, then randomly select 4 files


Dim PicFiles as  New Collection
Dim FileName as string
dim i as long
Dim Image1 as  String
Dim Image2 as String
Dim Image3 as String
Dim Image4 as String

'Read all picture files

FileName=Dir(*the picture folder*)
Do While Len(FileName) >0
    PicFiles.Add (*the picturefolder*)+"\"+FileName
    FileName=Dir()
Loop

'Select 4 unique random files

If PicFiles.Count >4 then
      Image1=PicFiles(1+(Rnd*(PicFiles.Count-1))
      
      Do While (Image2=Image1) or (Image2=Image3) or (Image2=Image4)
         Image2=PicFiles(1+(Rnd*(PicFiles.Count-1))
      Loop

      Do While (Image3=Image1) or (Image3=Image2) or (Image3=Image4)
         Image3=PicFiles(1+(Rnd*(PicFiles.Count-1))
      Loop

      Do While (Image4=Image1) or (Image4=Image2) or (Image4=Image3) or (Image4)=""
         Image4=PicFiles(1+(Rnd*(PicFiles.Count-1))
      Loop
Endif

'To load an image into picture box

Set PictureBox1.Picture=LoadPicture(image)


~Ajith
for the 2nd part of the question, how do i get 3 unique images, none of them which are a string (for example, none of them have the name strimage)
ignore last comment :)
Generates the path names of 3 random images :




Form1:
____________________

Dim imgPreexist(10) As Boolean, cnt As Integer
Private Sub Form_Activate()
DoEvents

    Do While cnt < 3 'generate 3 random images
       
        Randomize Timer
        gsx = CInt(Rnd * 8) + 1 'generate random  [something1 - something9.jpg]
       
           If imgPreexist(gsx) = False Then
                imgPreexist(gsx) = True: cnt = cnt + 1
                myRndImg = App.Path & "\something" & gsx & ".jpg"
                MsgBox myRndImg
                'Picture1.Picture = LoadPicture(myRndImg)
           End If
   
    DoEvents
    Loop
 
End Sub