Question Looping through images from a folder

RickyUser6

Member
Joined
Jan 27, 2006
Messages
7
Programming Experience
1-3
Hello there,

I've been searching for a while on how to correctly loop images from a folder.

My program consists of 6 pictureboxes that get populated by images from a folder in my computer.
I have a "Next" button that when clicked, shows the next 6 images.
My problem is, after I hit the "Next" button one more time, it doesn't cycle through the remaining pictures.

Example: I have 14 images in a folder. The program loads, it displays the first 6 images (1-6), I hit the "Next" button, and it displays the next 6 images (7-12). I hit the "Next" button once more, but the remaining images don't show (what's showing in the picturebox at this moment are pictures 7-12).

I was hoping someone could give me a hint on where the problem might be.

Here is part of my code:

VB.NET:
Dim pics() As PictureBox = {Card1, Card2, Card3, Card4, Card5, Card6}
Dim List() As String = Directory.GetFiles("C:\Users\blabla\Desktop\Country", "*.jpg")

For i As Integer = 0 To pics.Count - 1

     pics(i).Image = Image.FromFile(List(i + 6))

 Next

Anything fishy that you guys catch from the above code?

Thanks in advance!
 
LINQ is a bit of an advanced topic but the LINQ Skip and Take methods are very useful when working with items in pages:
Private Const PAGE_SIZE As Integer = 6

Private pageIndex As Integer = 0
Private imageFolderPath As String = "..."

Private Sub nextPageButton_Click(sender As Object, e As EventArgs) Handles nextPageButton.Click
    pageIndex += 1

    Dim images = GetCurrentPage()

    '...
End Sub

Private Sub previousPageButton_Click(sender As Object, e As EventArgs) Handles previousPageButton.Click
    pageIndex -= 1

    Dim images = GetCurrentPage()

    '...
End Sub

Private Function GetCurrentPage() As Image()
    'Get the full file list, skip all the files in pages prior to the current page and then take the files in the current page.
    Dim filePaths = Directory.GetFiles(imageFolderPath).Skip(pageIndex * PAGE_SIZE).Take(PAGE_SIZE).ToArray()

    Dim upperBound = filePaths.GetUpperBound(0)
    Dim images(upperBound) As Image

    For i = 0 To upperBound
        images(i) = Image.FromFile(filePaths(i))
    Next

    Return images
End Function
This code is not complete but demonstrates how you can accomplish the paging. One very important feature that it lacks is disposing of the outgoing page worth of Images before creating the new page worth of Images. It's always very important to dispose objects when you're done with them if they support it.
 
Hey thanks for the reply!

The LINQ Skip and Try does sound a bit confusing.

But I see you mentioned Disposing and that might be part of the problem I'm facing.

I tried having 8 images in my folder this time to see what would happen to the remaining pictureboxes that didn't get populated. The first 6 images load, then I click the "Next" button and the next 2 images show, BUT also the previous pictures. They don't get cleared.
I tried to do a Dispose or an Image = Nothing inside the loop but I can't seem to clear the old pictures.

This one clears the picturebox adjacent to the last new image, but the old ones remain after that one:
VB.NET:
For i As Integer = 0 To pics.Count - 1

     pics(i).Image = Nothing
     pics(i).Image = Image.FromFile(List(i + 6))

 Next

This one clears the new images coming in but the old images are still there haha:
VB.NET:
For i As Integer = 0 To pics.Count - 1

     pics(i).Dispose()
     pics(i).Image = Image.FromFile(List(i + 6))

 Next

I've tried placing the Dispose outside the loop, before and after and I'm still having problems.

Any help would be appreciated.

I wanted to give a shot to the LINQ but I just don't understand it that well just yet, I'll keep reading about it.
 
It's not Skip and Try; it's Skip and Take. They do just what they say they do. You have a list of items and you skip an number of them and then take a number of them. Let's say that you have 100 items and you want items 41 to 60. You would skip 40 and then take 20. As an example, try this:
'Get a list of 100 numbers starting at 1.
Dim numbers = Enumerable.Range(1, 100)

'Skip the first 40 numbers and then take the next 20.
Dim someNumbers = numbers.Skip(40).Take(20)

'Look at the numbers you have taken.
For Each number In someNumbers
    MessageBox.Show(number.ToString())
Next
As for clearing out the old Images, I'd suggest that you do that first, then load the new ones. Put all your PictureBoxes in an array and then clear each one first, then load the new Images:
Dim pictureBoxes = {PictureBox1, PictureBox2, ..., PictureBox6}

'Clear all PictureBoxes.
For Each pb In pictureBoxes
    pb.Image.Dispose()
    pb.Image = Nothing
Next

'Load the new Images.
For i = 0 To newImages.GetUpperBound(0)
    pictureBoxes(i).Image = newImages(i)
Next
That way it doesn't matter whether 'newImages' has six elements or fewer because you know that all the PictureBoxes are empty to begin with.
 
Back
Top