在 Windows XP 中,“FileInfo.LastWriteTime”将返回照片的拍摄日期 - 无论文件在文件系统中移动了多少次。
在 Vista 中,它返回从相机复制照片的日期。
在 Vista 中如何知道照片的拍摄时间?在 Windows 资源管理器中,此字段称为“拍摄日期”。
最佳答案
这是尽可能快速和干净的。通过使用 FileStream,您可以告诉 GDI+ 不要加载整个图像进行验证。它在我的机器上运行速度超过 10 倍。
//we init this once so that if the function is repeatedly called
//it isn't stressing the garbage man
private static Regex r = new Regex(":");
//retrieves the datetime WITHOUT loading the whole image
public static 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);
string dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
return DateTime.Parse(dateTaken);
}
}
是的,正确的 ID 是 36867,而不是 306。
下面的其他开源项目应该注意这一点。在处理数千个文件时,这是一个巨大的性能损失。
关于c# - 我怎样才能知道照片是在 Vista 上运行的 C# 中实际拍摄的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/180030/