SHARE
TWEET
Untitled
a guest
Jan 29th, 2018
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- //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);
- }
- }
- Image myImage = Image.FromFile(@"C:tempIMG_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);
- // Read all metadata from the image
- var directories = ImageMetadataReader.ReadMetadata(stream);
- // Find the so-called Exif "SubIFD" (which may be null)
- var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
- // Read the DateTime tag value
- var dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
- var directories = JpegMetadataReader.ReadMetadata(stream, new[] { new ExifReader() });
- //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 = null;
- try
- {
- propItem = myImage.GetPropertyItem(36867);
- }
- catch { }
- if (propItem != null)
- {
- string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
- return DateTime.Parse(dateTaken);
- }
- else
- return new FileInfo(path).LastWriteTime;
- }
- }
RAW Paste Data

