Question Image files list in listview

mk aidu

New member
Joined
Mar 30, 2011
Messages
4
Programming Experience
Beginner
i have code for filling a listbox with files with a specified extension(in this case, jpg files)

Imports System
Imports
System.IO

Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try

'Get jpg files
Dim
dirs As String() = Directory.GetFiles("c:\dave\", "*.jpg")
Dim dir As String
'Add file paths and filenames to the listbox
For Each dir In dirs ListBox1.Items.Add(dir)
Next

Catch ex As Exception
End Try
End Sub
--------------------------------------------
i'm thinking there's a way to do a similar thing using the listview.

i want to load image files into the listview. first column contains the filename, the next should contain the extension,the next the image size(pixels) and the last column should contain file size. pls help.

 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
It will be similar but, inside your loop, you'll have to do a bit more work. First, rather than using Directory.GetFiles, I would suggest creating a DirectoryInfo and using DirectoryInfo.GetFiles. That will return an array of FileInfo objects instead of just Strings, so you can get the file size from them too. As for the image size, there may be some unmanaged function you can use but the only managed method that I'm aware of is to create an Image object and get the dimensions from that.

As for adding the items to the ListView, the MSDN documentation for the ListView class has a code example. Reading the relevant documentation should always be the first thing you do. The documentation for the type or member you're using is always relevant.
 

mk aidu

New member
Joined
Mar 30, 2011
Messages
4
Programming Experience
Beginner
how to load selected filenames into listview?

I got an openfiledialog to open image files(filter property of the openfile dialog). But i don't know how to load the selected files into the listview. In the listview,
I got 4 columns(for Name, Extension,Image Size,File Size).


Imports System.IO
Public
Class Form1
Private Sub AddImageFilesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddImageFilesToolStripMenuItem.Click

Try

OpenFileDialog1.Title = "Load Files"
OpenFileDialog1.Filter = "JPG Files|*.jpg|GIF Files|*.gif|BITMAP Files|*.bmp|All Files|*.*"
OpenFileDialog1.FilterIndex = 1 OpenFileDialog1.ShowDialog()

Dim
files() As String = System.IO.Directory.GetFiles(OpenFileDialog1.FileName)

For Each f As String In files
Dim
info As New System.IO.FileInfo(f)
Dim
str(3) As String

str(0) = IO.Path.GetFileNameWithoutExtension(f)
str(1) = System.IO.Path.GetExtension(f)
str(3) = CStr(info.Length) & " Kb"

Using
img As Image = Image.FromFile(f)

str(2) = img.Size.Width & " x " & img.Size.Height & " px"
img.Dispose()

End Using str(3) = CStr(info.Length) & " Kb"

Dim
item As New ListViewItem(str)
vFiles.Items.Add(item)
Next

Catch ex As Exception
End Try
End Sub
End
Class

I think there's something wrong with this line in the above code
Dim files() As String = System.IO.Directory.GetFiles(OpenFileDialog1.FileName)
 

Joshdbr

Well-known member
Joined
Apr 7, 2011
Messages
211
Location
Montana, USA
Programming Experience
5-10
Try changing this line:

VB.NET:
Dim files() As String = System.IO.Directory.GetFiles(OpenFileDialog1.FileName)

To:

VB.NET:
Dim files() As String = OpenFileDialog1.FileNames

Hope this help!
-Josh
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,553
Location
Sydney, Australia
Programming Experience
10+
Either you want the user to select files or you want them to select a folder and then you'll get all the files from it. If it's the former then you've already got the file names, as Joshdbr has shown. If it's the latter then you should be using a FolderBrowserDialog rather than an OpenFileDialog, so the user can select a folder.
 
Top Bottom