Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load Image files from folder

I have a checked list box and a thumbnail area to display them where I am trying to load only images from a specific folder and need to display in thumbnails area but the problem is there is a thumbs.db file which is also being added to the checked list box which I don't need it.

So how do I actually load only the image files without the thumbs.db file.

Here is my code:

Private Sub LoadProjectToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoadProjectToolStripMenuItem.Click
    Using ofdlg As New Windows.Forms.OpenFileDialog

        ofdlg.DefaultExt = "trk"
        ofdlg.Filter = "Project|*.trk"
        ofdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
        If ofdlg.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim SaveData As New gCanvasData
            Using objStreamReader As New StreamReader(ofdlg.FileName)
                Dim x As New XmlSerializer(GetType(gCanvasData))
                SaveData = CType(x.Deserialize(objStreamReader), gCanvasData)
                objStreamReader.Close()
            End Using

            With SaveData
                'gTSSizer_gAZoom.Value = 100
                GCanvas1.ImageXYReset()
                GCanvas1.Image = .Image
                GCanvas1.gAnnotates = .gAnnotates
                GCanvas1.RebuildAll()
                GCanvas1.AssembleBitmap()
            End With

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

            For Each fi As FileInfo In New DirectoryInfo(fullpath).GetFiles
                CheckedListBox1.Items.Add(Application.StartupPath + "\" & System.IO.Path.GetFullPath(ofdlg.FileName))
                For i As Integer = 0 To CheckedListBox1.Items.Count - 1
                    CheckedListBox1.SetItemChecked(i, True)
                    ThumbControl1.AddFolder(fullpath, True)
                Next i
            Next
        End If
    End Using
End Sub
like image 731
coder Avatar asked Mar 02 '26 17:03

coder


2 Answers

Either filter it inside of the 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

or do it in the GetFiles:

DirectoryInfo(fullpath).GetFiles(".jpg")
like image 72
jor Avatar answered Mar 05 '26 07:03

jor


Found the solution at last:

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
like image 22
coder Avatar answered Mar 05 '26 06:03

coder