Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out when a picture was actually taken in C# running on Vista?

Tags:

c#

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".

like image 647
sepang Avatar asked Oct 07 '08 19:10

sepang


People also ask

How do you prove date and time of a photo?

Simple answer: you can't prove anything. A (digital) photo is just a sequence of ones and zeros (bits) on a disk somewhere, and anyone with enough time, money and dedication could get those bits to say anything they wanted.


2 Answers

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.

like image 132
kDar Avatar answered Sep 24 '22 20:09

kDar


Image myImage = Image.FromFile(@"C:\temp\IMG_0325.JPG"); PropertyItem propItem = myImage.GetPropertyItem(306); DateTime dtaken;  //Convert date taken metadata to a DateTime object string sdate = Encoding.UTF8.GetString(propItem.Value).Trim(); string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" "))); string firsthalf = sdate.Substring(0, 10); firsthalf = firsthalf.Replace(":", "-"); sdate = firsthalf + secondhalf; dtaken = DateTime.Parse(sdate); 
like image 32
sepang Avatar answered Sep 22 '22 20:09

sepang