更改照片的“DateTaken”

我刚刚从美国旅行回来,在编辑完所有照片之后,我发现相机使用的是以色列时区,而不是美国人。 有7个小时的时差,所以对我来说这是个大问题。 我有175GB的照片,但我只关心350张照片。 我无法手动编辑他们的EXIF,所以我想到了使用C#。

这个想法是它将读取每张照片的EXIF,获取时间,并在原始照片中设置时间减去7小时。 我尝试使用Image类,但它不起作用。 我尝试使用bitmapMetadate,它有效! 我已经设法得到时间,做了零下7个小时,但我不知道如何保存它。 我该怎么做? 谢谢!

public static string PhotoToBeEdited(FileInfo f) { FileStream fs = new FileStream(f.FullName, FileMode.Open, FileAccess.Read, FileShare.Read); BitmapSource img = BitmapFrame.Create(fs); BitmapMetadata md = (BitmapMetadata)img.Metadata; string date = md.DateTaken; Console.WriteLine(date); DateTime dt= DateTime.Parse(date); date = dt.AddHours(-7).ToString(); [...] return date; } 

我发现最简单的方法是使用此处描述的技术和System.Drawing.Bitmap;

代码应该是这样的:

  public void ChangeDateTaken(string path) { Image theImage = new Bitmap(path); PropertyItem[] propItems = theImage.PropertyItems; Encoding _Encoding = Encoding.UTF8; var DataTakenProperty1 = propItems.Where(a => a.Id.ToString("x") == "9004").FirstOrDefault(); var DataTakenProperty2 = propItems.Where(a => a.Id.ToString("x") == "9003").FirstOrDefault(); string originalDateString = _Encoding.GetString(DataTakenProperty1.Value); originalDateString = originalDateString.Remove(originalDateString.Length - 1); DateTime originalDate = DateTime.ParseExact(originalDateString, "yyyy:MM:dd HH:mm:ss", null); originalDate = originalDate.AddHours(-7); DataTakenProperty1.Value = _Encoding.GetBytes(originalDate.ToString("yyyy:MM:dd HH:mm:ss") + '\0'); DataTakenProperty2.Value = _Encoding.GetBytes(originalDate.ToString("yyyy:MM:dd HH:mm:ss") + '\0'); theImage.SetPropertyItem(DataTakenProperty1); theImage.SetPropertyItem(DataTakenProperty2); string new_path = System.IO.Path.GetDirectoryName(path) + "\\_" + System.IO.Path.GetFileName(path); theImage.Save(new_path); theImage.Dispose(); } 

不要忘记添加System.Drawing程序集。 如果需要,您可能还需要根据您的文化调整DateTime格式

不完全是编程解决方案,但您可以使用exiftool 。 我将它用于这个目的。

日期/时间转换function

你有没有忘记在拍摄一堆照片之前在数码相机上设置日期/时间? ExifTool具有时移function,可以轻松地将批量修复应用于图像的时间戳(例如,更改Windows资源管理器报告的“拍摄日期图片”)。 例如,当您在2005:11:03 10:48:00放入新电池时,您的相机时钟被重置为2000:01:01 00:00:00。 然后,您拍摄的所有照片的时间戳都会错误地显示5年,10个月,2天,10小时和48分钟。 要解决此问题,请将所有图像放在同一目录(“DIR”)中并运行exiftool:

 > exiftool "-DateTimeOriginal+=5:10:2 10:48:0" DIR 

您还可以设置TimeZoneOffset字段,以防有实际使用它的软件。