-
Feb 10th, 2012, 06:14 AM
#1
Thread Starter
Lively Member
[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:
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
Can anyone help??
Last edited by Superthijs; Feb 10th, 2012 at 09:42 AM.
-
Feb 10th, 2012, 08:50 AM
#2
Hyperactive Member
Re: Loading random image
On which line of your code does the error occur?
Rico
Using: VB.net & MS SQL
-
Feb 10th, 2012, 09:40 AM
#3
Thread Starter
Lively Member
Re: Loading random image
At this lines:
But this error doesn't occur very often.
Last edited by Superthijs; Feb 10th, 2012 at 09:43 AM.
-
Feb 10th, 2012, 10:21 AM
#4
Hyperactive Member
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
-
Feb 10th, 2012, 11:57 AM
#5
Thread Starter
Lively Member
Re: Loading random image
Ok, I'll try to get rid of the Goto's and see if that works better.
-
Feb 10th, 2012, 12:13 PM
#6
Thread Starter
Lively Member
Re: Loading random image
It still gives me that error...
-
Feb 10th, 2012, 12:21 PM
#7
Hyperactive Member
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:
Dim Folderpath as string = FolderBrowserDialog.SelectedPath Dim Files() As String = Directory.GetFiles(Folderpath) Dim Rnd As New Random Dim RandNo As Integer = Rnd.Next(0, UBound(Files)) 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
-
Feb 10th, 2012, 02:38 PM
#8
Addicted Member
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.
-
Feb 11th, 2012, 05:58 AM
#9
Thread Starter
Lively Member
Re: Loading random image
Thanks guys, I'll try that right now.
-
Feb 11th, 2012, 06:09 AM
#10
Thread Starter
Lively Member
Re: Loading random image
Well, I suddenly realise that my very long code wasn't neccesary at all.
This worked even better:
vb Code:
If FolderBrowserDialog.ShowDialog <> vbOK Then Exit Sub Dim Files As Array = IO.Directory.GetFiles(FolderBrowserDialog.SelectedPath) Dim NumFiles As Integer For Each File In Files NumFiles = Numfiles + 1 Next If NumFiles = 0 Then MsgBox("There are no supported images in the selected directory." & vbCrLf & _ "The .BMP, .GIF, .JPEG, .JPG and .PNG formats are supported only.", vbOKOnly + vbExclamation, "No supported images") OpenRandomImage() End If SelectRandomFile: Dim RandomNumber As Integer = Int(((NumFiles - 1) - 0 + 1) * Rnd()) Dim RandomFile As String = UCase(Files(RandomNumber)) 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 picPicture.ImageLocation = RandomFile UpdateImageLocation()
I'll get rid of the Goto later, this was only temporarily.
Thanks for your help guys, especially enrico1982.
-
Feb 11th, 2012, 07:10 AM
#11
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
-
Feb 11th, 2012, 08:06 AM
#12
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:
Option Strict On Imports System.IO Public Class RandomImage Private ReadOnly _acceptedExtensions() As String = {".jpg", ".jpeg", ".tif", ".bmp", ".gif", ".png"} Private ReadOnly _rng As New Random() Public Function RandomImage(ByVal path As String) As String Dim dir As New DirectoryInfo(path) Dim image As String = String.Empty While image = String.Empty Dim images = dir.GetFiles(String.Format("*{0}", RandomExtension), SearchOption.AllDirectories) If Not images.Count = 0 Then image = images(_rng.Next(0, images.Length - 1)).FullName End If End While Return image End Function Private Function RandomExtension() As String Return _acceptedExtensions(_rng.Next(0, 5)) End Function 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|