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.
Visual Basic Classic

Avatar of undefined
Last Comment
vb_elmar

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
vb_elmar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
Mike Tomlinson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
AjithJose


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
cachedVB

ASKER
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)
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
cachedVB

ASKER
ignore last comment :)
vb_elmar

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