Hello,
Recently I learned What you can do with an image list and now I wish to share this knowledge with you.
First Put;
- One image list component onto your form
- One picture box.
- One Timer (optinal)
- Two Buttons
- One Textbox
Once you have done that place the picture box in the center of your form and the buttons and the textbox at the bottom. Make sure one button is next to the textbox name it "set location" and the other "next".
First we need to enter some functions:
CODE
Private allowedExtensions() As String = { _
".gif", _
".jpg", _
".bmp", _
".png", _
".tiff" _
}
Function fillImlWithFilesFromDir(ByRef dirPath As String) As ImageList
Dim dirFiles() As String = IO.Directory.GetFiles(dirPath)
For Each dirFile As String In dirFiles
For Each extension As String In allowedExtensions
If extension = IO.Path.GetExtension(dirFile) Then
ImageList1.Images.Add(Image.FromFile(dirFile))
End If
Next
Next
Return ImageList1
End Function
Baisicly this bit of code:
CODE
Private allowedExtensions() As String = { _
".gif", _
".jpg", _
".bmp", _
".png", _
".tiff" _
}
sets the allowed extensions so when we set the target folder to get the images from we don't want a word document a text file and a HTML document finding their way in there do we

.
This bit of code:
CODE
Function fillImlWithFilesFromDir(ByRef dirPath As String) As ImageList
Dim dirFiles() As String = IO.Directory.GetFiles(dirPath)
For Each dirFile As String In dirFiles
For Each extension As String In allowedExtensions
If extension = IO.Path.GetExtension(dirFile) Then
ImageList1.Images.Add(Image.FromFile(dirFile))
End If
Next
Next
Return ImageList1
End Function
is the function we will use to populate the image list component. And once the file has been found (if it conforms to the above extensions then it gets added to the image list.
for you set location button enter the code:
CODE
ImageList1 = fillImlWithFilesFromDir(TextBox1.Text)
this basically calls the function we created earlier and sets the target directory as textbox1.text now I would also enclose this statement in an try catch block to prevent crashing. so the code would look like this:
CODE
Try
ImageList1 = fillImlWithFilesFromDir(TextBox1.Text)
Catch ex As Exception
MsgBox(ex.Message)
End Try
now for the next button add this code:
CODE
Static imageNumber As Integer = 0
imageNumber = (imageNumber + 1) Mod ImageList1.Images.Count
PictureBox1.Image = ImageList1.Images(imageNumber)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
What this code does is first create a number "imageNumber" then uses that to add one to the image number on the image list, this bit here:
CODE
PictureBox1.Image = ImageList1.Images(imageNumber)
is the actual putting the image into the picture box and this section of code:
CODE
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
makes sure the picture box doesnt expand with the image or crop the image.
And there you go. If you wanted to create a slideshow then you would create another button and use that to start the timer which has this code in it:
CODE
Static imageNumber As Integer = 0
imageNumber = (imageNumber + 1) Mod ImageList1.Images.Count
PictureBox1.Image = ImageList1.Images(imageNumber)
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
of course you could add some nice fading effects - but that's another tutorial

.
Enjoy,