Get EXIF date from photo
It’s not straightforward to retrieve the EXIF date from an image. Here’s a method to retrieve it using the MetadataExtractor package. It looks for the appropriate date, and if not found, falls back to the file properties.
// <package id="MetadataExtractor" version="2.0.0" targetFramework="net40" /> static DateTime GetImageDate(string file) { var directories = ImageMetadataReader.ReadMetadata(file); ExifSubIfdDirectory subIfdDirectory = null; try { subIfdDirectory = directories.OfType<ExifSubIfdDirectory>().FirstOrDefault(); } catch (Exception ex) { } DateTime? dateTime; try { dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTime); return dateTime.Value; } catch (Exception ex) { try { dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTimeOriginal); return dateTime.Value; } catch (Exception ex1) { try { dateTime = subIfdDirectory?.GetDateTime(ExifDirectoryBase.TagDateTimeDigitized); return dateTime.Value; } catch (Exception ex2) { dateTime = new[] { System.IO.File.GetCreationTime(file), System.IO.File.GetLastWriteTime(file) }.Min(); return dateTime.Value; } } } }
Comments
Post a Comment