I have 2 buttons, one is a browse button that allows you to choose which folder you want to select the images from and a GO button that gets a random image from that folder

here is what i've gotten so far:
Public Class directory

Private Sub browsebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles browsebtn.Click
    If (folderbrowse.ShowDialog() = DialogResult.OK) Then
    'texxt.Text is there to display what you've selected
        texxt.Text = folderbrowse.SelectedPath
    End If


End Sub

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
    Pic.ImageLocation = texxt.Text & "*.png"
    'Pic is the name of the picturebox


End Sub

End Class

Do you guys know how i can make it so that once i've chosen the direcotry (where the images i want to view are) and hit GO the picturebox displays one random .png or .jpg image?
I would also be interested in knowing how to make 2 other buttons to act as NEXT image (starting from the random pick) and a PREVIOUS image..

Thanks for reading!

Recommended Answers

All 3 Replies

Read the file names into a list. When you want to display a random picture, generate a random index number from 0 to FileList.Count - 1. If you use the Tag property of the PictureBox to store the currently displayed index then you can avoid a separate global variable. Your Prev, Next and Random buttons can simply set the index, then call ShowPic to load the image. As I found out the hard way, do not load the imiage directly into the PictureBox or you will lock the image file. Instead, do it this way...

Private Sub ShowPic()

    Dim fs As New System.IO.FileStream(FileList(pbxImage.Tag), IO.FileMode.Open, IO.FileAccess.Read)
    pbxImage.Image = System.Drawing.Image.FromStream(fs)
    fs.Close()

End Sub

Thanks for the quick reply, but i preffer knowing what those codes mean,
coudl you explain to me what they stand for so i can learn what they actually are instead of just copying them?

Sure. Just keep in mind I have no way of knowing your level of expertise. Feel free to ask for clarification if I'm glossing over anything.

Private FileList As New List(Of String)

This is the list for storing the file names. fs is a FileStream object for file I/O. The three parameters are

  1. file name
  2. operation (in this case, Open the file)
  3. access mode (Read)

    pbxImage.Image = System.Drawing.Image.FromStream(fs)

pbxImage is a picturebox control. This line reads the image file.

fs.Close()

This closes the connection between your applicatiion and the image file. As stated before, a logical place to keep track of the index of the file you are displaying is in the Tag property of the PictureBox control. Therefore

FileList(pbxImage.Tag)

returns the name of the file you want to display. You could also code the ShowPic sub as

Private Sub ShowPic(indx As Integer)

    pbxImage.Tag = indx

    Dim fs As New System.IO.FileStream(FileList(indx), IO.FileMode.Open, IO.FileAccess.Read)
    pbxImage.Image = System.Drawing.Image.FromStream(fs)
    fs.Close()

End Sub

Then your NextPic code would be something like

If pbxImage.Tag < FileList.Count - 1 Then
    ShpowPic(pbxImage.Tag + 1)
End If

I strongly recommend you get used to naming your controls for their function as in btnRandom, btnNext, btnPrev, etc. When you start building complex forms with (for example) lots of buttons then it will be much easier to follow

Private Sub btnRandom_Click(

than

Private Sub btn_Click(
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.