0

I have a pictureBox on my form along with two buttons (Back and Forward) but I can not find a viable method of doing what I wish to do: Scrolling through images in a folder like the default Windows Picture Viewer does with Arrow Keys.

Is there an efficient way to do this?

I'm using Visual Basic .NET with Visual Studio 2010, if that matters.

Quirk
  • 9
  • 1
  • 2

2 Answers2

1

You'll need to load the pictures using DirectoryInfo, then browse through them with an index. Here is an example:

Public Class Form1
    Private files As List(Of FileInfo)
    Private currentFileIndex As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshFolder("c:\path\to\your\pictures")
    End Sub

    Private Sub backButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backButton.Click
        Advance(-1)
        ShowCurrentFile()
    End Sub

    Private Sub forwardButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles forwardButton.Click
        Advance(1)
        ShowCurrentFile()
    End Sub

    Private Sub Advance(ByVal delta As Integer)
        currentFileIndex = ((currentFileIndex + files.Count) + delta) Mod files.Count
    End Sub

    Private Sub RefreshFolder(ByRef path As String)
        Dim di As DirectoryInfo = New DirectoryInfo(path)
        files = (From c In di.GetFiles()
                Where IsFileSupported(c)
                Select c).ToList()

        If files.Count > 0 Then
            currentFileIndex = 0
        End If

        ShowCurrentFile()
    End Sub

    Private Sub ShowCurrentFile()
        If currentFileIndex <> -1 Then
            Try
                PictureBox1.Image = Image.FromFile(files(currentFileIndex).FullName)
            Catch ex As Exception
                ' TODO: handle exceptions gracefully
                Debug.WriteLine(ex.ToString)
            End Try
        End If
    End Sub

    Private Function IsFileSupported(ByRef file As FileInfo) As Boolean
        Return file.Extension = ".jpg" Or file.Extension = ".png" ' etc
    End Function
End Class   
steinar
  • 9,383
  • 1
  • 23
  • 37
  • 1
    @Quirk: Then, dear friend, you're not a programmer. – steinar Mar 21 '11 at 01:21
  • There are so many undeclared items in that code that I'm questioning if you are. – Quirk Mar 21 '11 at 21:16
  • @Quirk: Undeclared!? What do you mean? If you mean that the declaration of the buttons are missing you've probably never read a GUI code example before. They're not needed as any halfwit knows the rest of the story. This code compiles and works. It does exactly what you are asking for. Getting a vulgar comment when answering a question fully is unheard on SO. I think it's pretty obvious that you don't know what you're talking about and if you don't understand this code you have got a *lot* to learn. That will be the last I have to say about this. – steinar Mar 21 '11 at 22:42
  • I'm not talking about the buttons or the controls on the form, I know what I'm doing. Your code doesn't compile because it's ordered like a buffoon. You can't call a function before you declare it. Don't call me a halfwit when you can't even get the order of the items correct. The error 'Object reference not set to an instance of an object.' occurs, anyways. – Quirk Mar 25 '11 at 23:29
  • You certainly can order the methods like this! You can order them anyway you want in a **class** in **VB.NET**. What language are you using?! That exception you mention is a runtime exception - never happens on compile time (that's where order matters), you don't even know the difference! Now go read some books on programming and learn some manners. This isn't the venue for juvenile nonsense like this. BTW: If you *read* the comment you would see I wasn't calling *you* a halfwit (although I'm now starting to think you are one). So I can only assume that you didn't read the code either. – steinar Mar 26 '11 at 00:16
0

you should be more specific. if it will help you you have to create two subrotines that assign the next and pervious image to the picture box and triggier these subrotines on the key down events and the bottons clicks.

Aladdin
  • 384
  • 1
  • 5