c# - 我怎样才能知道照片是在 Vista 上运行的 C# 中实际拍摄的?

标签 c#

在 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/

相关文章:

c# - 如何在 gridview 中使用 LinkBut​​ton 删除代码隐藏文件中的选定用户名?

c# - 在不使用临时变量的情况下交换两个变量

c# - 用于查看 C# 中的整数是否为素数的简单函数?

c# - 没有互联网的移动通知(仅限本地网络)

c# - 如何根据条件在 Wpf DataGrid 中的选定项目后获取项目?

c# - 在移动服务数据库上启用代码优先迁移时出错

c# - linq在groupby之后排序分组元素

c# - 将页面 url 设为页面标题

c# - 如何将字符串参数传递给 DELETE WebAPI 端点

c# - 结合完整的 URL 和虚拟 URL,就像浏览器一样