簡體   English   中英

VB.NET PictureBox滾動瀏覽文件夾中的圖像

[英]VB.NET PictureBox scroll through images in a folder

我的窗體上有一個pictureBox以及兩個按鈕(“后退”和“前進”),但是我找不到一種可行的方法來做自己想做的事情:像默認的Windows Picture Viewer中的箭頭鍵一樣,在文件夾中滾動圖像。

有一種有效的方法可以做到這一點嗎?

如果這很重要,我正在將Visual Basic .NET與Visual Studio 2010一起使用。

您需要使用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   

您應該更具體。 如果這將幫助您,則必須創建兩個子旋轉,將下一個和先前的圖像分配給圖片框,並在按下事件和底部單擊時觸發這些子旋轉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM