C# – How to find out when a picture was actually taken in C# running on Vista


In windows XP "FileInfo.LastWriteTime" will return the date a picture is taken – regardless of how many times the file is moved around in the filesystem.

In Vista it instead returns the date that the picture is copied from the camera.

How can I find out when a picture is taken in Vista? In windows explorer this field is referred to as "Date Taken".

Best Solution

Here's as fast and clean as you can get it. By using FileStream, you can tell GDI+ not to load the whole image for verification. It runs over 10 × as fast on my machine.

//we init this once so that if the function is repeatedly called
//it isn't stressing the garbage man
private static Regex r = new Regex(":");

//retrieves the datetime WITHOUT loading the whole image
public static DateTime GetDateTakenFromImage(string path)
    using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
    using (Image myImage = Image.FromStream(fs, false, false))
        PropertyItem propItem = myImage.GetPropertyItem(36867);
        string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
        return DateTime.Parse(dateTaken);

And yes, the correct id is 36867, not 306.

The other Open Source projects below should take note of this. It is a huge performance hit when processing thousands of files.