Private Sub OpenRandomImage() Handles mnuOpenRandom.Click
If FolderBrowserDialog.ShowDialog <> vbOK Then Exit Sub
Dim Path As String = FolderBrowserDialog.SelectedPath
Dim BmpFiles, GifFiles, JpegFiles, JpgFiles, PngFiles As Array
BmpFiles = IO.Directory.GetFiles(Path & "\", "*.bmp")
GifFiles = IO.Directory.GetFiles(Path & "\", "*.gif")
JpegFiles = IO.Directory.GetFiles(Path & "\", "*.jpeg")
JpgFiles = IO.Directory.GetFiles(Path & "\", "*.jpg")
PngFiles = IO.Directory.GetFiles(Path & "\", "*.png")
Dim Bmp, Gif, Jpeg, Jpg, Png As Integer
For Each File In BmpFiles
Bmp = Bmp + 1
Next
For Each File In GifFiles
Gif = Gif + 1
Next
For Each File In JpegFiles
Jpeg = Jpeg + 1
Next
For Each File In JpgFiles
Jpg = Jpg + 1
Next
For Each File In PngFiles
Png = Png + 1
Next
If Bmp = 0 And Gif = 0 And Jpeg = 0 And Jpg = 0 And Png = 0 Then
MsgBox("There are no supported images in the selected directory." & vbCrLf & _
"Supported images are of type .BMP, .GIF, .JPEG, .JPG and .PNG.", vbOKOnly + vbExclamation, "Warning")
OpenRandomImage()
End If
SelectRandomFile:
Dim RandomNumber As Integer = Int((4 - 0 + 1) * Rnd() + 0)
' Check if the files exist
If RandomNumber = 0 And Bmp = 0 Then GoTo SelectRandomFile
If RandomNumber = 1 And Gif = 0 Then GoTo SelectRandomFile
If RandomNumber = 2 And Jpeg = 0 Then GoTo SelectRandomFile
If RandomNumber = 3 And Jpg = 0 Then GoTo SelectRandomFile
If RandomNumber = 4 And Png = 0 Then GoTo SelectRandomFile
Dim a As Integer
Randomize()
' Prepare BMP files
If RandomNumber = 0 Then
Dim Files(Bmp - 1)
For Each File In BmpFiles
Files(a) = File
a = a + 1
Next
Randomize()
Dim SelectedFile As String = Files(Int(((Bmp - 1) - 0 + 1) * Rnd() + 0))
If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
ResetImage()
picPicture.ImageLocation = SelectedFile
On Error GoTo SelectRandomFile
End If
' Prepare GIF files
If RandomNumber = 1 Then
Dim Files(Gif - 1)
For Each File In GifFiles
Files(a) = File
a = a + 1
Next
Randomize()
Dim SelectedFile As String = Files(Int(((Gif - 1) - 0 + 1) * Rnd() + 0))
If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
ResetImage()
picPicture.ImageLocation = SelectedFile
End If
' Prepare JPEG files
If RandomNumber = 2 Then
Dim Files(Jpeg - 1)
For Each File In JpegFiles
Files(a) = File
a = a + 1
Next
Randomize()
Dim SelectedFile As String = Files(Int(((Jpeg - 1) - 0 + 1) * Rnd() + 0))
If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
ResetImage()
picPicture.ImageLocation = SelectedFile
On Error GoTo SelectRandomFile
End If
' Prepare JPG files
If RandomNumber = 3 Then
Dim Files(Jpg - 1)
For Each File In JpgFiles
Files(a) = File
a = a + 1
Next
Randomize()
Dim SelectedFile As String = Files(Int(((Jpg - 1) - 0 + 1) * Rnd() + 0))
If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
ResetImage()
picPicture.ImageLocation = SelectedFile
End If
' Prepare PNG files
If RandomNumber = 4 Then
Dim Files(Png - 1)
For Each File In PngFiles
Files(a) = File
a = a + 1
Next
Randomize()
Dim SelectedFile As String = Files(Int(((Png - 1) - 0 + 1) * Rnd() + 0))
If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
ResetImage()
picPicture.ImageLocation = SelectedFile
End If
lblStatus.Text = "Ready"
End Sub