C# 设置/创建 DateTaken 参数
C# Set / create DateTaken parameter
我打算用 C# 设置 DateTaken 参数,因为我有很多照片没有这个日期。
我只找到这条评论
在这个主题中,他们正在更改它而不是创建它。
如果使用此功能,但DataTakenProperty1或DataTakenProperty2为空,无法设置。
private static void SetDateTaken(string path, DateTime NEWdate)
{
Image theImage = new Bitmap(path);
PropertyItem[] propItems = theImage.PropertyItems;
Encoding _Encoding = Encoding.UTF8;
var DataTakenPropert = propItems.SetValue(NEWdate.ToString("yyyy:MM:dd HH:mm:ss"), ??How do i know the index??);
theImage.SetPropertyItem(DataTakenProperty);
string new_path = Path.GetDirectoryName(path) + "\_" + Path.GetFileName(path);
theImage.Save(new_path);
theImage.Dispose();
}
感谢您的帮助
您可以使用一些小技巧来实现此目的,因为当它为 null 时您无法启动 PropertyItem。
It is difficult to set property items, because the PropertyItem class
has no public constructors. One way to work around this restriction is
to obtain a PropertyItem by retrieving the PropertyItems property
value or calling the GetPropertyItem method of an Image that already
has property items. Then you can set the fields of the PropertyItem
and pass it to SetPropertyItem.
private static void SetDateTaken(string path, string samplePath, DateTime NEWdate)
{
Encoding _Encoding = Encoding.UTF8;
Image theImage = new Bitmap(path);
PropertyItem[] propItems = theImage.PropertyItems;
var DataTakenProperty1 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
var DataTakenProperty2 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
//// this is where you do the hack
if (DataTakenProperty1 == null)
{
Image sampleImage = new Bitmap(samplePath);
PropertyItem fakePropertyItem1 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
fakePropertyItem1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
fakePropertyItem1.Len = fakePropertyItem1.Value.Length;
theImage.SetPropertyItem(fakePropertyItem1);
PropertyItem fakePropertyItem2 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
fakePropertyItem2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
fakePropertyItem2.Len = fakePropertyItem2.Value.Length;
theImage.SetPropertyItem(fakePropertyItem2);
}
else
{
DataTakenProperty1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
DataTakenProperty2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
theImage.SetPropertyItem(DataTakenProperty1);
theImage.SetPropertyItem(DataTakenProperty2);
}
theImage.Save(newPath);
theImage.Dispose();
}
我打算用 C# 设置 DateTaken 参数,因为我有很多照片没有这个日期。
我只找到这条评论
如果使用此功能,但DataTakenProperty1或DataTakenProperty2为空,无法设置。
private static void SetDateTaken(string path, DateTime NEWdate)
{
Image theImage = new Bitmap(path);
PropertyItem[] propItems = theImage.PropertyItems;
Encoding _Encoding = Encoding.UTF8;
var DataTakenPropert = propItems.SetValue(NEWdate.ToString("yyyy:MM:dd HH:mm:ss"), ??How do i know the index??);
theImage.SetPropertyItem(DataTakenProperty);
string new_path = Path.GetDirectoryName(path) + "\_" + Path.GetFileName(path);
theImage.Save(new_path);
theImage.Dispose();
}
感谢您的帮助
您可以使用一些小技巧来实现此目的,因为当它为 null 时您无法启动 PropertyItem。
It is difficult to set property items, because the PropertyItem class has no public constructors. One way to work around this restriction is to obtain a PropertyItem by retrieving the PropertyItems property value or calling the GetPropertyItem method of an Image that already has property items. Then you can set the fields of the PropertyItem and pass it to SetPropertyItem.
private static void SetDateTaken(string path, string samplePath, DateTime NEWdate)
{
Encoding _Encoding = Encoding.UTF8;
Image theImage = new Bitmap(path);
PropertyItem[] propItems = theImage.PropertyItems;
var DataTakenProperty1 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
var DataTakenProperty2 = propItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
//// this is where you do the hack
if (DataTakenProperty1 == null)
{
Image sampleImage = new Bitmap(samplePath);
PropertyItem fakePropertyItem1 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9003");
fakePropertyItem1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
fakePropertyItem1.Len = fakePropertyItem1.Value.Length;
theImage.SetPropertyItem(fakePropertyItem1);
PropertyItem fakePropertyItem2 = sampleImage.PropertyItems.FirstOrDefault(a => a.Id.ToString("x") == "9004");
fakePropertyItem2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
fakePropertyItem2.Len = fakePropertyItem2.Value.Length;
theImage.SetPropertyItem(fakePropertyItem2);
}
else
{
DataTakenProperty1.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
DataTakenProperty2.Value = _Encoding.GetBytes(NEWdate.ToString("yyyy:MM:dd HH:mm:ss") + '[=10=]');
theImage.SetPropertyItem(DataTakenProperty1);
theImage.SetPropertyItem(DataTakenProperty2);
}
theImage.Save(newPath);
theImage.Dispose();
}