我刚刚从美国旅行回来,在编辑完所有照片后,我注意到相机使用的是以色列时区,而不是美国人。有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;
}
答案 0 :(得分:11)
我发现的最简单方法是使用技术描述here和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格式
答案 1 :(得分:2)
不完全是编程解决方案,但您可以使用exiftool。我将它用于这个目的。
日期/时间转换功能
您是否曾忘记在数码相机上设置日期/时间 在拍了一堆照片之前? ExifTool具有时移功能 这样可以轻松地将批量修复应用于时间戳 图像(例如,更改"拍摄日期图片"由Windows报告 资源管理器)。比如说你的相机时钟被重置为 2000:01:01 00:00:00当你在2005年11:03投入新电池时 10:48:00。然后你拍的所有照片都有了 时间戳错误5年,10个月,2天,10小时和 48分钟。要解决此问题,请将所有图像放在同一目录中 (" DIR")并运行exiftool:
> exiftool "-DateTimeOriginal+=5:10:2 10:48:0" DIR
您还可以设置TimeZoneOffset字段,以防有实际使用该软件的软件。
答案 2 :(得分:0)
对于.NET Core,我使用了NuGet包ExifLibNet(https://github.com/oozcitak/exiflibrary)
PM>安装软件包ExifLibNet
// using ExifLibrary;
var file = ImageFile.FromFile(filename);
file.Properties.Set(ExifTag.DateTimeDigitized, dateTime);
file.Properties.Set(ExifTag.DateTimeOriginal, dateTime);
file.Save(filename);