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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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*)+"\"+FileNa
FileName=Dir()
Loop
'Select 4 unique random files
If PicFiles.Count >4 then
Image1=PicFiles(1+(Rnd*(Pi
Do While (Image2=Image1) or (Image2=Image3) or (Image2=Image4)
Image2=PicFiles(1+(Rnd*(Pi
Loop
Do While (Image3=Image1) or (Image3=Image2) or (Image3=Image4)
Image3=PicFiles(1+(Rnd*(Pi
Loop
Do While (Image4=Image1) or (Image4=Image2) or (Image4=Image3) or (Image4)=""
Image4=PicFiles(1+(Rnd*(Pi
Loop
Endif
'To load an image into picture box
Set PictureBox1.Picture=LoadPi
~Ajith
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)
ASKER
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
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
ASKER