Results 1 to 12 of 12

Thread: [RESOLVED] Loading random image

  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.

  2. #2
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: Loading random image

    On which line of your code does the error occur?
    Rico

    Using: VB.net & MS SQL

  3. #3

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

    Re: Loading random image

    At this lines:
    VB Code:
    1. Files(a) = File

    But this error doesn't occur very often.
    Last edited by Superthijs; Feb 10th, 2012 at 09:43 AM.

  4. #4
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: Loading random image

    I can't quite get a handle this code (its friday!) But what is saying is that 'a' is higher than the amount of objects that are in 'Files'. Given that you are using a random number as a starting point for this, i would think that when the random number is in a certain range it is causing this error. Try using a fixed value as a starting point instead of a random number.

    A couple of side points:

    I would say this is a lot of code just to select a random file. I would say that something similar could be done in a handful of lines. i.e:

    Load all available image file paths from your directory
    Create an array containing these file paths
    Generate a random number between 0 and the size of the array.
    Select the file path from the array.

    Using gotos, is generally considered very bad practice. If you need to re-use code, contain it in a new Sub of function, and call that when required.
    Rico

    Using: VB.net & MS SQL

  5. #5

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

    Re: Loading random image

    Ok, I'll try to get rid of the Goto's and see if that works better.

  6. #6

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

    Re: Loading random image

    It still gives me that error...

  7. #7
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: Loading random image

    Just getting rid of the Goto lines isn't going to help.

    I would say ditch most of that code and go with:

    vb Code:
    1. Dim Folderpath as string = FolderBrowserDialog.SelectedPath
    2. Dim Files() As String = Directory.GetFiles(Folderpath)
    3. Dim Rnd As New Random
    4. Dim RandNo As Integer = Rnd.Next(0, UBound(Files))
    5.  
    6. Dim YourSelectedRandomfile As String = Files(RandNo)

    You will need to add some code to check their are some images in the selected folder, that should be just a case of looping through Files() for any with extension of your selected types.
    Rico

    Using: VB.net & MS SQL

  8. #8
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: Loading random image

    if u want this code to work i would suggest setting a = 0 after every for...next loop after a is declared as integer.

    the error is because it a keeps adding up in every for...next loop and is not reset.

  9. #9

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

    Re: Loading random image

    Thanks guys, I'll try that right now.

  10. #10

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

    Re: Loading random image

    Well, I suddenly realise that my very long code wasn't neccesary at all.
    This worked even better:

    vb Code:
    1. If FolderBrowserDialog.ShowDialog <> vbOK Then Exit Sub
    2.         Dim Files As Array = IO.Directory.GetFiles(FolderBrowserDialog.SelectedPath)
    3.         Dim NumFiles As Integer
    4.         For Each File In Files
    5.             NumFiles = Numfiles + 1
    6.         Next
    7.         If NumFiles = 0 Then
    8.             MsgBox("There are no supported images in the selected directory." & vbCrLf & _
    9.                    "The .BMP, .GIF, .JPEG, .JPG and .PNG formats are supported only.", vbOKOnly + vbExclamation, "No supported images")
    10.             OpenRandomImage()
    11.         End If
    12. SelectRandomFile:
    13.         Dim RandomNumber As Integer = Int(((NumFiles - 1) - 0 + 1) * Rnd())
    14.         Dim RandomFile As String = UCase(Files(RandomNumber))
    15.         If RandomFile.EndsWith(".BMP") = False And RandomFile.EndsWith(".GIF") = False And RandomFile.EndsWith(".JPEG") = False And RandomFile.EndsWith(".JPG") = False And RandomFile.EndsWith(".PNG") = False Then GoTo SelectRandomFile
    16.         picPicture.ImageLocation = RandomFile
    17.         UpdateImageLocation()

    I'll get rid of the Goto later, this was only temporarily.

    Thanks for your help guys, especially enrico1982.

  11. #11
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Loading random image

    You already have the count so why are you using an integer and looping the items?

    Code:
    If Files.Count = 0 Then

  12. #12
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Loading random image

    Thinking about it your example is accepting all extension's and using a GoTo statement to loop. This is pretty poor so let's create a class to only allow extension's we permit. For this we will be using the directory info class. I also thought it might be a neat idea to make the extension random also.

    An idea would be to add already used images to a List(of

    vb Code:
    1. Option Strict On
    2.  
    3. Imports System.IO
    4.  
    5. Public Class RandomImage
    6.  
    7. Private ReadOnly _acceptedExtensions() As String = {".jpg", ".jpeg", ".tif", ".bmp", ".gif", ".png"}
    8. Private ReadOnly _rng As New Random()
    9.  
    10. Public Function RandomImage(ByVal path As String) As String
    11.     Dim dir As New DirectoryInfo(path)
    12.     Dim image As String = String.Empty
    13.  
    14.      While image = String.Empty
    15.           Dim images = dir.GetFiles(String.Format("*{0}", RandomExtension), SearchOption.AllDirectories)
    16.           If Not images.Count = 0 Then
    17.              image = images(_rng.Next(0, images.Length - 1)).FullName
    18.           End If
    19.       End While
    20.  
    21.         Return image
    22.     End Function
    23.  
    24. Private Function RandomExtension() As String
    25.    Return _acceptedExtensions(_rng.Next(0, 5))
    26. End Function
    27.  
    28. End Class
    Last edited by ident; Feb 11th, 2012 at 08:31 AM. Reason: *edit noticed a slight misstake

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