我从图像中获得图像创建日期。我注意到一些图像抛出我的Property cannot be found.
错误,而相同的代码在其他图像上工作。
为例,我已经在大约40个图像上测试了我的代码,在那里我没有发现任何问题,但现在我正在不同的图像集上测试相同的代码,所有这些图像都抛出了这个错误。
代码:private DateTime GetDateTakenFromImage(string path)
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
using (Image myImage = Image.FromStream(fs, false, false))
{
PropertyItem propItem = myImage.GetPropertyItem(36867);//Throws error here
string dateTaken = new Regex(":").Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
return DateTime.Parse(dateTaken);
}
}
我已经检查了工作和非工作图像的图像属性,但我没有看到任何差异。
我在这里错过了什么?
该属性并不总是存在。首先检查以下内容:
if (myImage.PropertyIdList.Any(p => p == 36867))