You could get an out of memory exception if the file you're trying to load is not a valid image file. Please check what kind of file you're loading before setting picturebox's Image property. Also, setting the pattern to limit the search to a certain extension could help:
Dim folder As String = "d:\MyImageFiles" Dim pattern As String = "*.jpg"
Sorry I forgot to mention that I am trying to get image files for a slide show to open in picturebox and the only way that I have been able to do it is to specify the number of files in the folder.
Dim folder As String = "c:\" Dim pattern As String = "*.*"
' Find all files in selected folder Dim files() As String = System.IO.Directory.GetFiles(folder, pattern, IO.SearchOption.TopDirectoryOnly) ... ' Selected index is an index of the picture you want to display [from the files() array] Dim selectedIndex As Integer = 0 ' Load picture in the picturebox PictureBox1.Image = Image.FromFile(files(selectedIndex))
Now I am getting"Index was outside the bounds of the array"
Here are some array checks you can do before setting the image:
If files.Length = 0 Then MessageBox.Show("No pictures found") End If
If selectedIndex < files.GetLowerBound(0) Or selectedIndex > files.GetUpperBound(0) Then MessageBox.Show("Invalid selection") End If
you could read all the filenames from specified folder using My.Computer.FileSystem.GetFiles() method, like in the following code:
Dim folder As String = "c:\" Dim pattern As String = "*.*"
For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchTopLevelOnly, pattern) MessageBox.Show(file) Next
You can also set the pattern to return only those files that match your criteria.
Define Open.... Are they all text file and you want to get the contents and append them to say a textbox.
The general concept of looping around the files in the directory remains the same
Dim folder As String = "c:\" Dim pattern As String = "*.txt"
dim Contents as string = ""
For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchTopLevelOnly, pattern) Contents = My.Computer.FileSystem.ReadAllText(File) Textbox1.Text = textBox1.text & Contents
Next
If you want to open the files in there respective applications then
replace the bold lines with
Process.Start(File)
should open them up in the associated application - so txt files would open up instances of notepad, html files would open instances of IE, doc file - word documents.
Which would load the contents of the picturebox with the file. Of course the files in the folder need to be supported graphics file formats for the picturebox.
Looping
David Ing
I figured out the memory exception.
My dir is d: instead of c:
Now I am getting"Index was outside the bounds of the array"
I am still playing around with it though
jls
You could get an out of memory exception if the file you're trying to load is not a valid image file. Please check what kind of file you're loading before setting picturebox's Image property. Also, setting the pattern to limit the search to a certain extension could help:
Dim folder As String = "d:\MyImageFiles"Dim pattern As String = "*.jpg"
...
Andrej
RajaGanapathy
By the way thanks for the help.I am a complete beginner at this.I try to figure things out for myself but this has stumped me.
I am getting one picture to load now.
Do I use a timer or thread.sleep to get the slide show effect
ghostface039
MMMalik
mracuraintegra
I don`t want to get the file names ,I want to open them
GeorgeBH
Sorry I forgot to mention that I am trying to get image files for a slide show to open in picturebox and the only way that I have been able to do it is to specify the number of files in the folder.
HelpMe2006
If you're working with VS2003, try the following:
Dim folder As String = "c:\"Dim pattern As String = "*.*" ' Find all files in selected folder
Dim files() As String = System.IO.Directory.GetFiles(folder, pattern, IO.SearchOption.TopDirectoryOnly)
...
' Selected index is an index of the picture you want to display [from the files() array]
Dim selectedIndex As Integer = 0
' Load picture in the picturebox
PictureBox1.Image = Image.FromFile(files(selectedIndex))
Andrej
Thomas55
Here are some array checks you can do before setting the image:
If files.Length = 0 ThenMessageBox.Show("No pictures found")
End If If selectedIndex < files.GetLowerBound(0) Or selectedIndex > files.GetUpperBound(0) Then
MessageBox.Show("Invalid selection")
End If
Andrej
Darkside
Hi,
you could read all the filenames from specified folder using My.Computer.FileSystem.GetFiles() method, like in the following code:
Dim folder As String = "c:\"Dim pattern As String = "*.*" For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchTopLevelOnly, pattern)
MessageBox.Show(file)
Next
You can also set the pattern to return only those files that match your criteria.
Andrej
SOAC
Define Open.... Are they all text file and you want to get the contents and append them to say a textbox.
The general concept of looping around the files in the directory remains the same
Dim folder As String = "c:\"
Dim pattern As String = "*.txt"
dim Contents as string = ""
For Each file As String In My.Computer.FileSystem.GetFiles(folder, FileIO.SearchOption.SearchTopLevelOnly, pattern)
Contents = My.Computer.FileSystem.ReadAllText(File)
Textbox1.Text = textBox1.text & Contents
Next
If you want to open the files in there respective applications then
replace the bold lines with
Process.Start(File)
should open them up in the associated application - so txt files would open up instances of notepad, html files would open instances of IE, doc file - word documents.
Duane Haas
IO.Searchoption.TopDirectoryOnly.
SearchOption is not a member of IO
This is the error that I am getting,I have played around with it and can`t seem to figure it out.
VBScriptor
Ok, .NET 1.x doesn't support SearchOption enumeration; just leave it out:
Dim files() As String = System.IO.Directory.GetFiles(folder, pattern)
Andrej
GazCoder
Now things are a bit clearer as to what your intention was.
You would simply replace
Contents = My.Computer.FileSystem.ReadAllText(File)
Textbox1.Text = textBox1.text & Contents
with
Picturebox1.Load (file)
Which would load the contents of the picturebox with the file. Of course the files in the folder need to be supported graphics file formats for the picturebox.