从文件夹加载图像文件

时间:2012-10-25 14:35:43

标签: vb.net winforms image load

我有一个checked list box和一个thumbnail area来显示它们我试图只加载来自特定文件夹的图片并需要显示在缩略图区域,但问题是有{{1}这个文件也被添加到了我不需要它的复选列表框中。

那么如何在没有thumbs.db文件的情况下实际加载图像文件。

这是我的代码:

thumbs.db

3 个答案:

答案 0 :(得分:2)

在For Each Loop中过滤它:

For Each fi As FileInfo In New DirectoryInfo(fullpath).GetFiles
    If Not {".jpg", ".png", ".bmp"}.Contains(fi.Extension) Then Continue For
    ' ...
Next

或在GetFiles中执行:

DirectoryInfo(fullpath).GetFiles(".jpg")

答案 1 :(得分:2)

最后找到了解决方案:

Dim fullpath As String
fullpath = Application.StartupPath & "\" & System.IO.Path.GetFileNameWithoutExtension(ofdlg.FileName) + "\"


Dim FileDirectory As New IO.DirectoryInfo(fullpath)
Dim FileJpg As IO.FileInfo() = FileDirectory.GetFiles("*.jpg")
Dim FileGif As IO.FileInfo() = FileDirectory.GetFiles("*.gif")
Dim FileBmp As IO.FileInfo() = FileDirectory.GetFiles("*.bmp")

For Each File As IO.FileInfo In FileJpg
CheckedListBox1.Items.Add(File.FullName)
Dim str As String
str = Directory.GetCurrentDirectory() & "\" & "Backup\"
       Next
         For Each File As IO.FileInfo In FileGif
                CheckedListBox1.Items.Add(File.FullName)
                Dim str As String
                str = Directory.GetCurrentDirectory() & "\" & "Backup\"
            Next
            For Each File As IO.FileInfo In FileBmp
                CheckedListBox1.Items.Add(File.FullName)
                Dim str As String
                str = Directory.GetCurrentDirectory() & "\" & "Backup\"
            Next
            For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                CheckedListBox1.SetItemChecked(i, True)
            Next i

答案 2 :(得分:0)

将DirectoryInfo(完整路径).GetFiles更改为DirectoryInfo(fullpath).EnumerateFiles()并为所需的图像文件扩展名添加搜索模式。
http://msdn.microsoft.com/en-us/library/dd383574.aspx