Speed up Camera Photo Time Stamp

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:MM:DD 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:MM:DD 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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Shane said:
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?

You are reading the entire image, which includes reading the entire file
and decompressing the image into a bitmap.

You can read the metadata directly from the file instead. That of course
means that you have to read up on the format of the file to find out how
to get to the interresting parts.

Alternatively you might find someone else who has already written code
for that.
 
S

Shane

Ok, yeah, that's pretty obvious that the image variable was the
slowing down area.

I could use a background task to update the dates in my list, but I'd
rather not.

Does anybody have faster code?

Shane
 
A

Andrew Morton

Shane said:
Ok, yeah, that's pretty obvious that the image variable was the
slowing down area.

I could use a background task to update the dates in my list, but I'd
rather not.

Does anybody have faster code?

The file is likely to be in a format similar to TIFF or JFIF [0] in which
there are "segments" of data. You should be able to read in the first (say)
32KB of the file and traverse the segments until you get to the one with the
XMP data [1]. Then you can extract the required data with an xquery [2][3].

That will be vastly faster.

I have code to extract some XMP metadata, but it is too embarrasingly messy
to show anyone.


[0] If you can get hold of the Adobe Photoshop 6.0 SDK, the format is fairly
completely documented in it; you can also try www.wotsit.org for file format
documentation.

[1] Go through the segments until you find the APP1 one with the namespace
field containing http://ns.adobe.com/xap/1.0/

[2] You may need to get rid of the namespaces in <namespace:name> and
</namespace/name> for xquery to work.

[3] XQuery info at http://aspnet.4guysfromrolla.com/articles/071603-1.aspx
XQuery.msi available from
http://aspnet.4guysfromrolla.com/code/XQueryStuff.zip
However, xquery may be built into the 2.0 .net framework.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top