Here's a code snippet from an old program I have lying around that does something very similar to this:
string dateTakenText;
using (Image photo = Image.FromFile(file.Name))
{
PropertyItem pi = photo.GetPropertyItem(Program.propertyTagExifDTOrig_);
ASCIIEncoding enc = new ASCIIEncoding();
dateTakenText = enc.GetString(pi.Value, 0, pi.Len - 1);
}
if (string.IsNullOrEmpty(dateTakenText))
{
continue;
}
DateTime dateTaken;
if (!DateTime.TryParseExact(dateTakenText, "yyyy:MM:dd HH:mm:ss",
CultureInfo.CurrentCulture, DateTimeStyles.None, out dateTaken))
{
continue;
}
This code snippet sits inside a foreach
loop, which explains the use of the continue
keyword.
This is code from a program I wrote sometime back in 2002 or 2003, and I've been using it regularly since then; it works pretty reliably.