我有点惊讶和不解。我尝试从图像中读取属性项。特别是,我对“拍摄日期”感兴趣。我已经编写了一个程序来执行此操作。或多或少。对于某些文件,它可以完美地工作,但是......
我有一些文件在属性中有一个“拍摄日期”(当通过 Windows 资源管理器、Windows 7 x64 查看时)。它们不同于创建、修改和访问的日期。所以我确实有第四次约会。 但是,如果我遍历属性项,它不会显示(在任何 ID 上)。 当我在 PropertyItem.Id(0x9003 或 36867)上查找它时,我发现属性项不存在。
我的循环遍历属性项的代码:
for (int i = 0; i < fileNames.Length; i++)
{
FileStream fs = new FileStream(fileNames[i], FileMode.Open, FileAccess.Read);
Image pic = Image.FromStream(fs, false, false);
int t = 0;
foreach (PropertyItem pii in pic.PropertyItems)
{
MessageBox.Show(encoding.GetString(pii.Value, 0, pii.Len - 1) + " - ID: " + t.ToString());
t++;
}
}
只读取“拍摄日期”属性的代码(我从这里偷了:http://snipplr.com/view/25074/)
public static DateTime DateTaken(Image getImage)
{
int DateTakenValue = 0x9003; //36867;
if (!getImage.PropertyIdList.Contains(DateTakenValue))
return DateTime.Parse("01-01-2000");
string dateTakenTag = System.Text.Encoding.ASCII.GetString(getImage.GetPropertyItem(DateTakenValue).Value);
string[] parts = dateTakenTag.Split(':', ' ');
int year = int.Parse(parts[0]);
int month = int.Parse(parts[1]);
int day = int.Parse(parts[2]);
int hour = int.Parse(parts[3]);
int minute = int.Parse(parts[4]);
int second = int.Parse(parts[5]);
return new DateTime(year, month, day, hour, minute, second);
}
但是,当我在 Windows 资源管理器的“文件属性窗口”中更改日期时,它开始显示在我的程序中。
所以我的问题是:这个“拍摄日期”从何而来?我怎样才能访问它?难不成除了EFIX数据之外还有别的信息来源?
谢谢!
最佳答案
如果你想从一些基本的编码开始,你可以尝试这样的事情
//随心所欲地加载图像。 System.Drawing.Image image = new Bitmap("我的图片.jpg");
Referenced from AbbydonKrafts
// Get the Date Created property
//System.Drawing.Imaging.PropertyItem propertyItem = image.GetPropertyItem( 0x132 );
System.Drawing.Imaging.PropertyItem propertyItem
= image.PropertyItems.FirstOrDefault(i => i.Id == 0x132 );
if( propItem != null )
{
// Extract the property value as a String.
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
string text = encoding.GetString(propertyItem.Value, 0, propertyItem.Len - 1 );
// Parse the date and time.
System.Globalization.CultureInfo provider = CultureInfo.InvariantCulture;
DateTime dateCreated = DateTime.ParseExact( text, "yyyy:MM:d H:m:s", provider );
}
关于c# - "Date Taken"在 Windows 资源管理器中显示在文件详细信息(文件属性)中时未显示在 Image PropertyItems 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14308284/