Picture Array

sarahxiaori

Member
Joined
Apr 29, 2012
Messages
6
Programming Experience
Beginner
Hi,

I am new to VB.NET and i have a question about image array.

I have a folder that contain many photos(sand.jpg,sea.jpg, flower.jpg....etc etc) and i only need to load the first 12 photo from the folder.
Here is my code:
VB.NET:
Dim PictureBoxes() = {PictureBox0, PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10, PictureBox11}
        Dim PictureFiles(11) As Image
     
         

        Dim i As Integer = 0
        For i = 0 To 11
            PictureFiles(i) = Image.FromFile("D:\mis\photo\*.jpg")
        Next

        Dim a As Integer = 0
        For a = 0 To 11
            PictureBoxes(a).Image = PictureFiles(a)

        Next


Error Occur : Illegal characters in path.


Please help:grumpy:
 
Image.FromFile requires and actual file path. Do you have a file named "*.jpg"? Obviously not. If you want to use a pattern to get multiple file paths then you need to call the GetFiles method of the Directory class. You can then loop through that array of paths and pass each one to Image.FromFile.

By the way, why have two loops? Why can't you just perform both tasks in the one loop? In fact, you don't even need to use Image.FromFile at all. You can just call the Load method of the PictureBox to load directly from the file. That's better because it doesn't keep the file open, which Image.FromFile does and I'll wager you're not allowing for that.
 
Hi jmcilhinney,
Thank for you reply .
I had change my code to

VB.NET:
Dim PictureBoxes() = {PictureBox0, PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10, PictureBox11}
        Dim PictureFiles(11) As Image

        Dim i As Integer = 0
        For i = 0 To 11
            
            PictureFiles(i) = Directory.GetFiles("D:\mis\photo\", "*.jpg")
PictureBoxes(i).Image = PictureFiles(i)

        Next

but I get error : Error 1 Value of type '1-dimensional array of String' cannot be converted to 'System.Drawing.Image'. D:\misdotnet\MISDOTNET\MISDOTNET\SelectImage.vb 13 31 MISDOTNET

Can advice?
 
Look at the name of the GetFiles method. What do you think it does?

VB.NET:
Dim PictureBoxes() = {PictureBox0, PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6, PictureBox7, PictureBox8, PictureBox9, PictureBox10, PictureBox11}
        Dim i As Integer = 0
        For i = 0 To 11
            Dim List() As String = Directory.GetFiles("D:\mis\photo\", "*.jpg")
            PictureBoxes(i).Image = Image.FromFile(List(i))
        Next

I did it!!!! Thank you sooooo much ❤
 
That will work but it's not quite ideal. You are calling GetFiles inside the loop, which means that you're getting the same list of file paths 12 times. Anything that doesn't need to be repeated, e.g. getting the list that you want to loop through, doesn't belong inside the loop. Also, what's going to happen there if there are less than 12 files in the folder?
 
That will work but it's not quite ideal. You are calling GetFiles inside the loop, which means that you're getting the same list of file paths 12 times. Anything that doesn't need to be repeated, e.g. getting the list that you want to loop through, doesn't belong inside the loop. Also, what's going to happen there if there are less than 12 files in the folder?


Hi jmcilhinney,
thanks again for your reply.
I had change the path before entering the for loop so it wont loop for 12 times. Is this way more ideal?


VB.NET:
Dim aPictureBoxes() = {picWallpaper0, picWallpaper1, picWallpaper2, picWallpaper3, picWallpaper4, picWallpaper5, picWallpaper6, picWallpaper7, picWallpaper8, picWallpaper9, picWallpaper10, picWallpaper11}
        Dim sLabelName() = {lblWallpaper0, lblWallpaper1, lblWallpaper2, lblWallpaper3, lblWallpaper4, lblWallpaper5, lblWallpaper6, lblWallpaper7, lblWallpaper8, lblWallpaper9, lblWallpaper10, lblWallpaper11}
        Dim i As Integer = 0
        Dim sPictureList() As String = Directory.GetFiles("M:\PRG\WIN\Wallpaper\1920-1080 16.9\", "*.jpg")

        For i = 0 To 11
            aPictureBoxes(i).Image = Image.FromFile(sPictureList(i))
            Dim sDirName As String = IO.Path.GetFileName(sPictureList(i))
            sLabelName(i).Text = sDirName.Substring(0, sDirName.Length - 4)
        Next

Beside, I had a new problem now with the repeated code of the image clicked, the code is as below:
VB.NET:
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper1.Click
        frmMain.BackgroundImage = picWallpaper1.Image
    End Sub

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper2.Click
        frmMain.BackgroundImage = picWallpaper2.Image
    End Sub

    Private Sub PictureBox3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper3.Click
        frmMain.BackgroundImage = picWallpaper3.Image
    End Sub

    Private Sub PictureBox4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper4.Click
        frmMain.BackgroundImage = picWallpaper4.Image
    End Sub

    Private Sub PictureBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper5.Click
        frmMain.BackgroundImage = picWallpaper5.Image
    End Sub

    Private Sub PictureBox6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper6.Click
        frmMain.BackgroundImage = picWallpaper6.Image
    End Sub

    Private Sub PictureBox7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper7.Click
        frmMain.BackgroundImage = picWallpaper7.Image
    End Sub

    Private Sub PictureBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper8.Click
        frmMain.BackgroundImage = picWallpaper8.Image
    End Sub

    Private Sub PictureBox9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper9.Click
        frmMain.BackgroundImage = picWallpaper9.Image
    End Sub

    Private Sub PictureBox0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper0.Click
        frmMain.BackgroundImage = picWallpaper0.Image
    End Sub

    Private Sub PictureBox10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper10.Click
        frmMain.BackgroundImage = picWallpaper10.Image
    End Sub

    Private Sub PictureBox11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picWallpaper11.Click
        frmMain.BackgroundImage = picWallpaper11.Image
    End Sub

but this way is not effective if I want to increase more image up to 50,60 in future the program will loaded with these code. Any suggestion?
 
You only need one Click event handler for all PictureBoxes. You can get reference to the PictureBox that was clicked from the 'sender' parameter and then get its Image property.

By the way, if you're calling GetFileName, why are you assigning the result to a variable named sDirName? Also, you appear to actually want the file name without the extension. The Path class has a method for that. Always read the documentation first and you won't miss useful members.
 
Back
Top