VB.NET PictureBox滚动文件夹中的图像

时间:2011-03-20 22:40:10

标签: vb.net scroll picturebox

我的表单上有一个pictureBox以及两个按钮(后退和前进)但我找不到一个可行的方法来做我想做的事情:滚动浏览文件夹中的图像,如默认的Windows图片查看器与箭头一样密钥。

有一种有效的方法吗?

如果重要的话,我在Visual Studio 2010中使用Visual Basic .NET。

2 个答案:

答案 0 :(得分:1)

您需要使用DirectoryInfo加载图片,然后使用索引浏览它们。这是一个例子:

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   

答案 1 :(得分:0)

你应该更具体。 如果它会帮助你你必须创建两个子程序,将下一个和前一个图像分配给图片框,并在按键事件和底部点击上触发这些子程序。