S
Shane
I'm writing a program for renaming my picture files, and I want to use
the picture time stamp as a prefix to the file names. I can get the
time stamp, but it is extraordinarily slow! I have about 250 pictures
in the directory. In debug mode, I can populate the names in a
listview in less than two seconds. If I add a column with the time
stamp as a string, the listview takes about 70 seconds to populate in
debug mode. I haven't made and test a release mode yet.
Does anybody know a faster way to get the time stamp information?
'---------- Extract the picture date from the picture ----------
' Return an empty string if the file does not exist or no date tag
' Returns date string in YYYY:MMD HH:MM:SS
Private Function GetRawDate(ByVal FileName As String) As String
Dim l_RawDate As String = ""
If File.Exists(FileName) Then
Try
Dim img As Image = Image.FromFile(FileName)
Dim i As PropertyItem
For Each i In img.PropertyItems
If (i.Id = &H9003 Or i.Id = &H9004) And i.Type = 2
Then
l_RawDate =
System.Text.Encoding.Default.GetString(i.Value, 0, i.Value.Length - 1)
Exit For
End If
Next
Catch ex As Exception
End Try
End If
Return l_RawDate
End Function
'---------- Get the date only in YY-MM-DD format ----------
' FullDate is in YYYY:MMD HH:MM:SS format
' Returns date string in YY-MM-DD format
Private Function GetPictureYYMMDD(ByVal FullDate As String) As
String
Dim l_YYMMDD As String = ""
If FullDate.Length >= 10 Then
l_YYMMDD = FullDate.Substring(2, 2) + "-" + _
FullDate.Substring(5, 2) + "-" + _
FullDate.Substring(8, 2)
End If
Return l_YYMMDD
End Function
'---------- Populate the listview ----------
Private Sub Populate()
Dim l_Directory As String = txtDirectory.Text
lvw.BeginUpdate()
lvw.Clear()
lblSelected.Text = "0 Selected"
If My.Computer.FileSystem.DirectoryExists(l_Directory) = False
Then
MsgBox("Directory name is invalid.",
MsgBoxStyle.Exclamation)
Else
Dim l_File As String
Dim l_Item As ListViewItem
Dim l_FileInfo As System.IO.FileInfo
Dim s As String = ""
lvw.Columns.Add("File Name")
lvw.Columns.Add("Picture Taken")
lvw.Columns.Add("Last Edited Date")
lvw.Columns.Add("Preview")
Try
For Each l_File In
My.Computer.FileSystem.GetFiles(l_Directory)
l_Item = New ListViewItem(l_File)
l_FileInfo = New System.IO.FileInfo(l_File)
s = GetRawDate(l_File)
s = GetPictureYYMMDD(s)
l_Item.SubItems.Add(s)
lvw.Items.Add(l_Item)
Next
Catch ex As Exception
End Try
End If
lvw.EndUpdate()
End Sub
the picture time stamp as a prefix to the file names. I can get the
time stamp, but it is extraordinarily slow! I have about 250 pictures
in the directory. In debug mode, I can populate the names in a
listview in less than two seconds. If I add a column with the time
stamp as a string, the listview takes about 70 seconds to populate in
debug mode. I haven't made and test a release mode yet.
Does anybody know a faster way to get the time stamp information?
'---------- Extract the picture date from the picture ----------
' Return an empty string if the file does not exist or no date tag
' Returns date string in YYYY:MMD HH:MM:SS
Private Function GetRawDate(ByVal FileName As String) As String
Dim l_RawDate As String = ""
If File.Exists(FileName) Then
Try
Dim img As Image = Image.FromFile(FileName)
Dim i As PropertyItem
For Each i In img.PropertyItems
If (i.Id = &H9003 Or i.Id = &H9004) And i.Type = 2
Then
l_RawDate =
System.Text.Encoding.Default.GetString(i.Value, 0, i.Value.Length - 1)
Exit For
End If
Next
Catch ex As Exception
End Try
End If
Return l_RawDate
End Function
'---------- Get the date only in YY-MM-DD format ----------
' FullDate is in YYYY:MMD HH:MM:SS format
' Returns date string in YY-MM-DD format
Private Function GetPictureYYMMDD(ByVal FullDate As String) As
String
Dim l_YYMMDD As String = ""
If FullDate.Length >= 10 Then
l_YYMMDD = FullDate.Substring(2, 2) + "-" + _
FullDate.Substring(5, 2) + "-" + _
FullDate.Substring(8, 2)
End If
Return l_YYMMDD
End Function
'---------- Populate the listview ----------
Private Sub Populate()
Dim l_Directory As String = txtDirectory.Text
lvw.BeginUpdate()
lvw.Clear()
lblSelected.Text = "0 Selected"
If My.Computer.FileSystem.DirectoryExists(l_Directory) = False
Then
MsgBox("Directory name is invalid.",
MsgBoxStyle.Exclamation)
Else
Dim l_File As String
Dim l_Item As ListViewItem
Dim l_FileInfo As System.IO.FileInfo
Dim s As String = ""
lvw.Columns.Add("File Name")
lvw.Columns.Add("Picture Taken")
lvw.Columns.Add("Last Edited Date")
lvw.Columns.Add("Preview")
Try
For Each l_File In
My.Computer.FileSystem.GetFiles(l_Directory)
l_Item = New ListViewItem(l_File)
l_FileInfo = New System.IO.FileInfo(l_File)
s = GetRawDate(l_File)
s = GetPictureYYMMDD(s)
l_Item.SubItems.Add(s)
lvw.Items.Add(l_Item)
Next
Catch ex As Exception
End Try
End If
lvw.EndUpdate()
End Sub