【问题标题】:Get Taken Date in EXIF property from Bitmap从位图中获取 EXIF 属性中的拍摄日期
【发布时间】:2017-12-23 07:53:03
【问题描述】:

我有一个分析图像的应用程序。 我必须检索图像的拍摄日期。我使用这个功能:

var r = new Regex(":");
var myImage = LoadImageNoLock(path);
{
    PropertyItem propItem = null;
    try
    {
        propItem = myImage.GetPropertyItem(36867);
    }
    catch{
        try
        {
            propItem = myImage.GetPropertyItem(306);
        }
        catch { }
    }
    if (propItem != null)
    {
        var dateTaken = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
        return DateTime.Parse(dateTaken);
    }
    else
    {
        return null;
    }
}

我的应用程序可以很好地处理相机拍摄的照片。 但现在,我像这样从网络摄像头保存照片:

private void Webcam_PhotoTakenEvent(Bitmap inImage)
{
    // Save photo on disk
    if (_takePhoto == true)
    {
        // Save the photo on disk
        inImage.Save(_currentPath + "/BV_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".jpeg");
    }
}

在这种情况下,我之前的函数不起作用,因为图像文件不包含任何 PropertyItem。

当我们手动保存图像时,有什么方法可以检索 PropertyItem 拍摄的日期?

提前致谢。

【问题讨论】:

  • filename.Split('_')[1] 有日期哈哈,不过更严重的如果你自己保存并去属性是日期正确还是没有日期?
  • GetPropertyItem(36867) 得到它我敢打赌SetPropertyItem() 得到它。
  • 你是对的@EpicKip,我可以返回新的 FileInfo(path).LastWriteTime ,而不是返回 null 。如果我想将照片移动到另一个地方,问题仍然存在。没有?
  • @SimonBross 那是个笑话建议的萌芽,看看亚历克斯在说什么

标签: c# bitmap exif


【解决方案1】:

终于在Alex的评论下找到了解决办法。

我手动设置了 PropertyItem:

private void Webcam_PhotoTakenEvent(Bitmap inImage)
{
     // Set the Date Taken in the EXIF Metadata
     var newItem = (PropertyItem)FormatterServices.GetUninitializedObject(typeof(PropertyItem));
     newItem.Id = 36867; // Taken date
     newItem.Type = 2;
     // The format is important the decode the date correctly in the futur
     newItem.Value =  System.Text.Encoding.UTF8.GetBytes(DateTime.Now.ToString("yyyy:MM:dd HH:mm:ss") + "\0");
     newItem.Len = newItem.Value.Length;
     inImage.SetPropertyItem(newItem);
     // Save the photo on disk
     inImage.Save(_currentPath + "/BV_" + DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss") + ".jpeg");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-20
    • 2012-12-06
    • 2013-05-29
    • 2016-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多