Results 1 to 12 of 12

Thread: [RESOLVED] Loading random image

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2011
    Location
    The Netherlands
    Posts
    84

    Resolved [RESOLVED] Loading random image

    Hi,
    I have made a function which opens a random image, but sometimes I get the 'Index out of bounds of array' error.

    Code:

    VB Code:
    1. Private Sub OpenRandomImage() Handles mnuOpenRandom.Click
    2.         If FolderBrowserDialog.ShowDialog <> vbOK Then Exit Sub
    3.         Dim Path As String = FolderBrowserDialog.SelectedPath
    4.         Dim BmpFiles, GifFiles, JpegFiles, JpgFiles, PngFiles As Array
    5.         BmpFiles = IO.Directory.GetFiles(Path & "\", "*.bmp")
    6.         GifFiles = IO.Directory.GetFiles(Path & "\", "*.gif")
    7.         JpegFiles = IO.Directory.GetFiles(Path & "\", "*.jpeg")
    8.         JpgFiles = IO.Directory.GetFiles(Path & "\", "*.jpg")
    9.         PngFiles = IO.Directory.GetFiles(Path & "\", "*.png")
    10.         Dim Bmp, Gif, Jpeg, Jpg, Png As Integer
    11.         For Each File In BmpFiles
    12.             Bmp = Bmp + 1
    13.         Next
    14.         For Each File In GifFiles
    15.             Gif = Gif + 1
    16.         Next
    17.         For Each File In JpegFiles
    18.             Jpeg = Jpeg + 1
    19.         Next
    20.         For Each File In JpgFiles
    21.             Jpg = Jpg + 1
    22.         Next
    23.         For Each File In PngFiles
    24.             Png = Png + 1
    25.         Next
    26.         If Bmp = 0 And Gif = 0 And Jpeg = 0 And Jpg = 0 And Png = 0 Then
    27.             MsgBox("There are no supported images in the selected directory." & vbCrLf & _
    28.                    "Supported images are of type .BMP, .GIF, .JPEG, .JPG and .PNG.", vbOKOnly + vbExclamation, "Warning")
    29.             OpenRandomImage()
    30.         End If
    31. SelectRandomFile:
    32.         Dim RandomNumber As Integer = Int((4 - 0 + 1) * Rnd() + 0)
    33.         ' Check if the files exist
    34.         If RandomNumber = 0 And Bmp = 0 Then GoTo SelectRandomFile
    35.         If RandomNumber = 1 And Gif = 0 Then GoTo SelectRandomFile
    36.         If RandomNumber = 2 And Jpeg = 0 Then GoTo SelectRandomFile
    37.         If RandomNumber = 3 And Jpg = 0 Then GoTo SelectRandomFile
    38.         If RandomNumber = 4 And Png = 0 Then GoTo SelectRandomFile
    39.         Dim a As Integer
    40.         Randomize()
    41.         ' Prepare BMP files
    42.         If RandomNumber = 0 Then
    43.             Dim Files(Bmp - 1)
    44.             For Each File In BmpFiles
    45.                 Files(a) = File
    46.                 a = a + 1
    47.             Next
    48.             Randomize()
    49.             Dim SelectedFile As String = Files(Int(((Bmp - 1) - 0 + 1) * Rnd() + 0))
    50.             If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
    51.             ResetImage()
    52.             picPicture.ImageLocation = SelectedFile
    53.             On Error GoTo SelectRandomFile
    54.         End If
    55.         ' Prepare GIF files
    56.         If RandomNumber = 1 Then
    57.             Dim Files(Gif - 1)
    58.             For Each File In GifFiles
    59.                 Files(a) = File
    60.                 a = a + 1
    61.             Next
    62.             Randomize()
    63.             Dim SelectedFile As String = Files(Int(((Gif - 1) - 0 + 1) * Rnd() + 0))
    64.             If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
    65.             ResetImage()
    66.             picPicture.ImageLocation = SelectedFile
    67.         End If
    68.         ' Prepare JPEG files
    69.         If RandomNumber = 2 Then
    70.             Dim Files(Jpeg - 1)
    71.             For Each File In JpegFiles
    72.                 Files(a) = File
    73.                 a = a + 1
    74.             Next
    75.             Randomize()
    76.             Dim SelectedFile As String = Files(Int(((Jpeg - 1) - 0 + 1) * Rnd() + 0))
    77.             If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
    78.             ResetImage()
    79.             picPicture.ImageLocation = SelectedFile
    80.             On Error GoTo SelectRandomFile
    81.         End If
    82.         ' Prepare JPG files
    83.         If RandomNumber = 3 Then
    84.             Dim Files(Jpg - 1)
    85.             For Each File In JpgFiles
    86.                 Files(a) = File
    87.                 a = a + 1
    88.             Next
    89.             Randomize()
    90.             Dim SelectedFile As String = Files(Int(((Jpg - 1) - 0 + 1) * Rnd() + 0))
    91.             If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
    92.             ResetImage()
    93.             picPicture.ImageLocation = SelectedFile
    94.         End If
    95.         ' Prepare PNG files
    96.         If RandomNumber = 4 Then
    97.             Dim Files(Png - 1)
    98.             For Each File In PngFiles
    99.                 Files(a) = File
    100.                 a = a + 1
    101.             Next
    102.             Randomize()
    103.             Dim SelectedFile As String = Files(Int(((Png - 1) - 0 + 1) * Rnd() + 0))
    104.             If SelectedFile = picPicture.ImageLocation Then GoTo SelectRandomFile
    105.             ResetImage()
    106.             picPicture.ImageLocation = SelectedFile
    107.         End If
    108.         lblStatus.Text = "Ready"
    109.     End Sub

    Can anyone help??
    Last edited by Superthijs; Feb 10th, 2012 at 09:42 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width