daily pastebin goal
40%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //we init this once so that if the function is repeatedly called
  2.     //it isn't stressing the garbage man
  3.     private static Regex r = new Regex(":");
  4.  
  5.     //retrieves the datetime WITHOUT loading the whole image
  6.     public static DateTime GetDateTakenFromImage(string path)
  7.     {
  8.         using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
  9.         using (Image myImage = Image.FromStream(fs, false, false))
  10.         {
  11.             PropertyItem propItem = myImage.GetPropertyItem(36867);
  12.             string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
  13.             return DateTime.Parse(dateTaken);
  14.         }
  15.     }
  16.    
  17. Image myImage = Image.FromFile(@"C:tempIMG_0325.JPG");
  18. PropertyItem propItem = myImage.GetPropertyItem(306);
  19. DateTime dtaken;
  20.  
  21. //Convert date taken metadata to a DateTime object
  22. string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
  23. string secondhalf = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
  24. string firsthalf = sdate.Substring(0, 10);
  25. firsthalf = firsthalf.Replace(":", "-");
  26. sdate = firsthalf + secondhalf;
  27. dtaken = DateTime.Parse(sdate);
  28.    
  29. // Read all metadata from the image
  30. var directories = ImageMetadataReader.ReadMetadata(stream);
  31.  
  32. // Find the so-called Exif "SubIFD" (which may be null)
  33. var subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault();
  34.  
  35. // Read the DateTime tag value
  36. var dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal);
  37.    
  38. var directories = JpegMetadataReader.ReadMetadata(stream, new[] { new ExifReader() });
  39.    
  40. //retrieves the datetime WITHOUT loading the whole image
  41.     public static DateTime GetDateTakenFromImage(string path)
  42.     {
  43.         using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
  44.         using (Image myImage = Image.FromStream(fs, false, false))
  45.         {
  46.             PropertyItem propItem = null;
  47.             try
  48.             {
  49.                 propItem = myImage.GetPropertyItem(36867);
  50.             }
  51.             catch { }
  52.             if (propItem != null)
  53.             {
  54.                 string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
  55.                 return DateTime.Parse(dateTaken);
  56.             }
  57.             else
  58.                 return new FileInfo(path).LastWriteTime;
  59.         }
  60.     }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top