public static void ProcessImage(String filePath, DateTime newDateTime) { ExifFile image = ExifFile.Read(filePath); //Set the EXIF date/time values in the image and save it. try { image.Properties[ExifTag.DateTimeDigitized].Value = newDateTime; } catch (KeyNotFoundException) //No DateTimeDigitized? Add one. { var dateTime = new ExifDateTime(ExifTag.DateTimeDigitized, newDateTime); image.Properties.Add(ExifTag.DateTimeDigitized, dateTime); } try { image.Properties[ExifTag.DateTime].Value = newDateTime; } catch(KeyNotFoundException) //No DateTime? Add one. { var dateTime = new ExifDateTime(ExifTag.DateTime, newDateTime); image.Properties.Add(ExifTag.DateTime, dateTime); } try { image.Properties[ExifTag.DateTimeOriginal].Value = newDateTime; } catch (KeyNotFoundException) //No DateTimeOriginal? Add one. { var dateTimeOriginal = new ExifDateTime(ExifTag.DateTimeOriginal, newDateTime); image.Properties.Add(ExifTag.DateTimeOriginal, dateTimeOriginal); } try { image.Properties[ExifTag.ThumbnailDateTime].Value = newDateTime; } catch (KeyNotFoundException) //No ThumbnailDateTime? Add one. { var thumbnailDate = new ExifDateTime(ExifTag.ThumbnailDateTime, newDateTime); image.Properties.Add(ExifTag.ThumbnailDateTime, thumbnailDate); } image.Save(filePath); //Update the images' file creation time. File.SetCreationTime(filePath, newDateTime); }
public static void ProcessImage(String filePath) { ExifFile image = ExifFile.Read(filePath); DateTime whenDigitised = DateTime.MinValue; try { whenDigitised = (DateTime)image.Properties[ExifTag.DateTimeDigitized].Value; } catch (KeyNotFoundException) //EXIF data doesn't contain a DateTimeDigitized flag { return; } //Correct the EXIF values in the image and save it. try { image.Properties[ExifTag.DateTime].Value = whenDigitised; } catch (KeyNotFoundException) //No DateTime? Add one. { var dateTime = new ExifDateTime(ExifTag.DateTime, whenDigitised); image.Properties.Add(ExifTag.DateTime, dateTime); } try { image.Properties[ExifTag.DateTimeOriginal].Value = whenDigitised; } catch (KeyNotFoundException) //No DateTimeOriginal? Add one. { var dateTimeOriginal = new ExifDateTime(ExifTag.DateTimeOriginal, whenDigitised); image.Properties.Add(ExifTag.DateTimeOriginal, dateTimeOriginal); } try { image.Properties[ExifTag.ThumbnailDateTime].Value = whenDigitised; } catch (KeyNotFoundException) //No ThumbnailDateTime? Add one. { var thumbnailDate = new ExifDateTime(ExifTag.ThumbnailDateTime, whenDigitised); image.Properties.Add(ExifTag.ThumbnailDateTime, thumbnailDate); } image.Save(filePath); //Update the images' file creation time. File.SetCreationTime(filePath, whenDigitised); }
public static void ProcessImage(String filePath, DateTime newDateTime) { ExifFile image = ExifFile.Read(filePath); //Set the EXIF date/time values in the image and save it. try { image.Properties[ExifTag.DateTimeDigitized].Value = newDateTime; } catch (KeyNotFoundException) //No DateTimeDigitized? Add one. { var dateTime = new ExifDateTime(ExifTag.DateTimeDigitized, newDateTime); image.Properties.Add(ExifTag.DateTimeDigitized, dateTime); } try { image.Properties[ExifTag.DateTime].Value = newDateTime; } catch (KeyNotFoundException) //No DateTime? Add one. { var dateTime = new ExifDateTime(ExifTag.DateTime, newDateTime); image.Properties.Add(ExifTag.DateTime, dateTime); } try { image.Properties[ExifTag.DateTimeOriginal].Value = newDateTime; } catch (KeyNotFoundException) //No DateTimeOriginal? Add one. { var dateTimeOriginal = new ExifDateTime(ExifTag.DateTimeOriginal, newDateTime); image.Properties.Add(ExifTag.DateTimeOriginal, dateTimeOriginal); } try { image.Properties[ExifTag.ThumbnailDateTime].Value = newDateTime; } catch (KeyNotFoundException) //No ThumbnailDateTime? Add one. { var thumbnailDate = new ExifDateTime(ExifTag.ThumbnailDateTime, newDateTime); image.Properties.Add(ExifTag.ThumbnailDateTime, thumbnailDate); } image.Save(filePath); //Update the images' file creation time. File.SetCreationTime(filePath, newDateTime); }
private static (DateTime CreateDateTime, string sCreateDateTime) ImageEXIF(FileInfo fileInfo) { DateTime _CreateDateTime = new DateTime(1753, 1, 1); string _sCreateDateTime = "Date not found"; ImageFile _file; string mess; // try to convert the file into a EXIF ImageFile try { _file = ImageFile.FromFile(fileInfo.FullName); } catch (NotValidImageFileException) { _sCreateDateTime = "Not valid image"; mess = $"{DateTime.Now}, WARN - File: {fileInfo.FullName}, _sCreateDateTime: {_sCreateDateTime}, _CreateDateTime: {_CreateDateTime}"; Log(mess); return(CreateDateTime : _CreateDateTime, sCreateDateTime : _sCreateDateTime); } catch (Exception exc) { _sCreateDateTime = "ERROR"; mess = $"{DateTime.Now}, ERR - File: {fileInfo.FullName}\r\n{exc.ToString()}\r\n"; Log(mess); return(CreateDateTime : _CreateDateTime, sCreateDateTime : _sCreateDateTime); } ExifDateTime _dateTag = _file.Properties.Get <ExifDateTime>(ExifTag.DateTime); if (_dateTag != null) { _sCreateDateTime = _dateTag.ToString(); if (DateTime.TryParse(_sCreateDateTime, out _CreateDateTime)) { if (_CreateDateTime == DateTime.MinValue) { _CreateDateTime = new DateTime(1753, 1, 1); } } } return(CreateDateTime : _CreateDateTime, sCreateDateTime : _sCreateDateTime); }
public static T GetQuery <T>(this BitmapMetadata bitmapMetadata, string query) { // Return default if the BitmapMetadata doesn't contain the query // Would prefer to return null if (!bitmapMetadata.ContainsQuery(query)) { return(default(T)); } // Grab object object unknownObject = bitmapMetadata.GetQuery(query); if (unknownObject == null) { return(default(T)); } else if (typeof(T) == typeof(SRational)) { if (unknownObject.GetType() == bitmapMetadata.GetStorageType(typeof(SRational))) { // Create new Rational, casting the unknownobject as an Int64 SRational rational = new SRational((Int64)unknownObject); // Convert back to typeof(T) return((T)Convert.ChangeType(rational, typeof(T))); } else { return(default(T)); } } else if (typeof(T) == typeof(URational)) { if (unknownObject.GetType() == bitmapMetadata.GetStorageType(typeof(URational))) { // Create new URational, casting the unknownobject as an UInt64 URational urational = new URational((UInt64)unknownObject); // Convert back to typeof(T) return((T)Convert.ChangeType(urational, typeof(T))); } else { return(default(T)); } } else if (typeof(T) == typeof(URationalTriplet)) { if (unknownObject.GetType() == bitmapMetadata.GetStorageType(typeof(URationalTriplet))) { // Create new GpsRational, casting the unknownobject as an Int64[] URationalTriplet gpsRational = new URationalTriplet((UInt64[])unknownObject); // Convert back to typeof(T) return((T)Convert.ChangeType(gpsRational, typeof(T))); } else { return(default(T)); } } else if (typeof(T) == typeof(ExifDateTime)) { // Create new ExifDateTime, casting the unknownobject as a string ExifDateTime exifDateTime = new ExifDateTime(unknownObject.ToString()); // Convert back to typeof(T) return((T)Convert.ChangeType(exifDateTime, typeof(T))); } else if (typeof(T) == typeof(TimeSpan)) { string timespanString = (unknownObject as string); if (!timespanString.EndsWith("+0000")) { throw new NotImplementedException("Timespan contains timezone, need to implement the right code"); } else if (timespanString.Length > 6) { int hour = Convert.ToInt32(timespanString.Substring(0, 2)); int minute = Convert.ToInt32(timespanString.Substring(2, 2)); int second = Convert.ToInt32(timespanString.Substring(4, 2)); TimeSpan timeSpan = new TimeSpan(hour, minute, second); return((T)Convert.ChangeType(timeSpan, typeof(T))); } else { return(default(T)); } } else if (typeof(T) == typeof(DateTime)) { // Split the string into date & time // Convert T to a space string[] dateTimeString = (unknownObject as string).Replace("T", " ").Split(' '); if (dateTimeString.Length == 1 && dateTimeString[0].Length == 8) { int year = Convert.ToInt32(dateTimeString[0].Substring(0, 4)); int month = Convert.ToInt32(dateTimeString[0].Substring(4, 2)); int day = Convert.ToInt32(dateTimeString[0].Substring(6, 2)); DateTime dateTime = new DateTime(year, month, day); return((T)Convert.ChangeType(dateTime, typeof(T))); } else if (dateTimeString.Length == 2) { // Ensure seperate is dash for Date dateTimeString[0] = dateTimeString[0].Replace(":", "-"); // Strip the Z from the Time dateTimeString[1] = dateTimeString[1].TrimEnd('Z'); DateTime dateTime = DateTime.Parse(dateTimeString[0] + " " + dateTimeString[1]); // Parse as local time DateTime localDateTime = new DateTime(dateTime.Ticks, DateTimeKind.Local); // Convert back to typeof(T) return((T)Convert.ChangeType(localDateTime, typeof(T))); } return(default(T)); } else if (typeof(T) == typeof(string)) { // Trim the string return((T)Convert.ChangeType(unknownObject.ToString().Trim(), typeof(T))); } else if (!typeof(T).IsAssignableFrom(unknownObject.GetType())) { // Throw exception if the object is the wrong type throw new System.ArgumentException("Query \"" + query + "\" has type \"" + unknownObject.GetType().ToString() + "\" not expected type \"" + typeof(T).ToString() + "\""); } return((T)unknownObject); }