Add several .jpg's from file to image array

Growler

Member
Joined
Sep 23, 2010
Messages
13
Programming Experience
Beginner
I'll keep this short and simple... I asked this question before, but It wasn't answered completely. I'd like to get several images from a folder, and add them to an array. This is not working, can someone explicitly tell me how to do it? Thanks!

At the top, I have:
VB.NET:
Dim dir = New IO.FileInfo("C:\Users\turcotd\Desktop\MyImageFolder\") 
Dim images = dir.GetFiles("*.jpg", IO.SearchOption.AllDirectories) 
Dim imageArray As New ArrayList()

On Form load: ADD IMAGES TO ARRAY
VB.NET:
For Each img As Image In images  
imageArray.Add(img)  
Next

On Add Button click: DISPLAY EACH IMAGE FROM ARRAY IN PICTURE PICTUREBOX(i)
VB.NET:
If (i < 6) Then 
pictureArray(i).Load(imageArray(i)) 
i = i + 1 
End If

Thanks!!!
 
First up, you should not be using an ArrayList at all. Either use an array or a List(Of T).

Second, why are you creating a FileInfo to represent a directory? A FileInfo is for files. A DirectoryInfo is for directories.

As for the issue, juts look at your code and ask yourself if it makes sense. Start with this line:
VB.NET:
pictureArray(i).Load(imageArray(i))
What does the PictureBox.Load method expect you to pass it? You don't need to look any further than Intellisense. It expects a String, which is supposed to contain the path of the image file. What are you passing it? Again, you don't have to look any further than Intellisense. What does 'imageArray' contain? I can tell you that it's not Strings.
 
First up, you should not be using an ArrayList at all. Either use an array or a List(Of T).

Second, why are you creating a FileInfo to represent a directory? A FileInfo is for files. A DirectoryInfo is for directories.

As for the issue, juts look at your code and ask yourself if it makes sense. Start with this line:
VB.NET:
pictureArray(i).Load(imageArray(i))
What does the PictureBox.Load method expect you to pass it? You don't need to look any further than Intellisense. It expects a String, which is supposed to contain the path of the image file. What are you passing it? Again, you don't have to look any further than Intellisense. What does 'imageArray' contain? I can tell you that it's not Strings.

I was using FileInfo because someone recommended I use it. I'm new to programming, and wasn't sure of the difference.
As for the Load method taking a String, that was a silly mistake on my end, but I felt like I was trying to pass in everything else (including an array of strings) and it wasnt working.

I've found a solution that works for now, but it's not dynamic...
and it's not the solution I'd like in the long run:
VB.NET:
 Dim ImageList() As String = {"C:\Users\turcotd\Desktop\ITLPers\itlp1.jpg", "C:\Users\turcotd\Desktop\ITLPers\itlp2.jpg", "etc.jpg..."}
and then just do:
VB.NET:
pictureArray(i).Load(ImageList(i))

I'd like to just have the picture box dynamically read the image files based on their position in the array
 
The DirectoryInfo.GetFiles method returns an array of FileInfo objects. A FileInfo object contains, as the name suggests, information about a file, including the path in the its FullName property. The Directory.GetFiles method returns an array of String objects. Each String contains the path of a file.
 
VB.NET:
        'Add images to List(Of T) from a file as string 

        Dim PictureArray As New List(Of Image)
        For Each item As String In Directory.GetFiles("C:\Users\Home\Desktop", "*.jpg", IO.SearchOption.AllDirectories)
            Dim _Image As Image = Image.FromFile(item)
            PictureArray.Add(_Image)
        Next

        'Usage

        For Each item As Image In PictureArray
            Dim pb As New PictureBox
            pb.Image = item
            FlowLayoutPanel1.Controls.Add(pb)
        Next

Or you could skip adding it to a List and go straight to the PictureBox

VB.NET:
        For Each item As String In Directory.GetFiles("C:\Users\Home\Desktop", "*.jpg", IO.SearchOption.AllDirectories)
            Dim pb As New PictureBox
            pb.Load(item)
            FlowLayoutPanel1.Controls.Add(pb)
        Next
 
s1ckOh, hey, thanks for the response.but Id like to load each image into a picturebox on my form (picBox1, picBox2,...) only when a button is clicked. Btw, if I were to add pic boxes to form via for loop like you did, how would I choose their location on the form?
 
Hmm... Well if you wanted to add it right to the form you would use the PictureBox.Location property, but that isn't easy to set in a For...Next loop. Easiest to use a FlowLayoutPanel because it lays them out neatly in order and you can set a scroll bar if you have to many images.

Say you wanted to set the location manually, you could get the x,y coordinates at the mouse click location then set the PictureBox location to that, eg...

VB.NET:
    Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        Dim _Point As New System.Drawing.Point
        _Point.X = e.X
        _Point.Y = e.Y

        Dim pb As New PictureBox
        pb.Load("Path to image or load from array")
        pb.Location = _Point

        Me.Controls.Add(pb)
    End Sub

but that is a bit beyond the scope of the original topic.
 
Thats pretty cool, but this is gettin way too complicated for what I wanted to achieve. All I'm lookin to do is 1st) add images to list/array, whatever. 2nd) each time I click "add" button, it adds a pic from that list/array of images directly to an i'th picturebox[]. I can add all the images to a list using a for loop, but cannot use a for loop to add the pics from the list to a picturebox(i), because the pic only appears in the picturebox on the form when you click add. Like in above code
 
Add the image to a List

VB.NET:
        Dim PictureArray As New List(Of Image)
        For Each item As String In Directory.GetFiles("C:\Users\Home\Desktop", "*.jpg", IO.SearchOption.AllDirectories)
            Dim _Image As Image = Image.FromFile(item)
            PictureArray.Add(_Image)
        Next

Add a picture from the array to each picturebox on your form.

VB.NET:
        Dim i As Integer = 0
        For Each item As Object In Me.Controls
            If TypeOf item Is PictureBox Then
                item.Image = PictureArray(i)
                i += 1
            End If
        Next
 
Perfect.

And to add it without using for loop, I've altered the code to the following:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PictureArray As New List(Of Image)
        For Each item As String In Directory.GetFiles("C:\Users\turcotd\Desktop\ITLPers", "*.jpg", IO.SearchOption.AllDirectories)
            Dim _Image As Image = Image.FromFile(item)
            PictureArray.Add(_Image)
        Next

        If (i < 6) Then
            Dim pb As New PictureBox
            Me.FlowLayoutPanel1.Controls.Add(pb)
            pb.Image = PictureArray(i)
            i = i + 1
        End If
    End Sub
 
Perfect.

And to add it without using for loop, I've altered the code to the following:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim PictureArray As New List(Of Image)
        For Each item As String In Directory.GetFiles("C:\Users\turcotd\Desktop\ITLPers", "*.jpg", IO.SearchOption.AllDirectories)
            Dim _Image As Image = Image.FromFile(item)
            PictureArray.Add(_Image)
        Next

        If (i < 6) Then
            Dim pb As New PictureBox
            Me.FlowLayoutPanel1.Controls.Add(pb)
            pb.Image = PictureArray(i)
            i = i + 1
        End If
    End Sub
There's no point in loading any of those images, and also not disposing image object before releasing last reference is very bad. The code refactors to this:
        Dim paths = IO.Directory.GetFiles("C:\Users\turcotd\Desktop\ITLPers", "*.jpg", IO.SearchOption.AllDirectories)
        If (i < 6) Then
            Dim pb As New PictureBox
            pb.ImageLocation = paths(i)
            Me.FlowLayoutPanel1.Controls.Add(pb)
            i += 1
        End If
 
There's no point in loading any of those images, and also not disposing image object before releasing last reference is very bad. The code refactors to this:
        Dim paths = IO.Directory.GetFiles("C:\Users\turcotd\Desktop\ITLPers", "*.jpg", IO.SearchOption.AllDirectories)
        If (i < 6) Then
            Dim pb As New PictureBox
            pb.ImageLocation = paths(i)
            Me.FlowLayoutPanel1.Controls.Add(pb)
            i += 1
        End If
@JohnH,

Thanks John. I've been looking to accomplish this with as few lines of code as possible, just wasn't aware of such available syntax.
 
Back
Top