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

发布于 2024-10-25 17:04:41 字数 181 浏览 2 评论 0原文

我的表单上有一个图片框以及两个按钮(后退和前进),但我找不到可行的方法来执行我想要执行的操作:在文件夹中滚动图像,就像默认的 Windows 图片查看器使用箭头键所做的那样。

有没有有效的方法来做到这一点?

如果重要的话,我将 Visual Basic .NET 与 Visual Studio 2010 一起使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就像说晚安 2024-11-01 17:04:42

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

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   
风为裳 2024-11-01 17:04:42

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文