首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Bitmap获取EXIF属性中的日期

从Bitmap获取EXIF属性中的日期
EN

Stack Overflow用户
提问于 2017-07-18 12:31:22
回答 1查看 994关注 0票数 0

我有一个分析图像的应用程序。我得收回一张照片的日期。我使用这个函数:

代码语言:javascript
运行
复制
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;
    }
}

我的申请与相机拍摄的照片配合得很好。但是现在,我从这样的网络摄像头中保存照片:

代码语言:javascript
运行
复制
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所取的日期吗?

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-18 18:39:42

最后,我找到了一个解决方案与亚历克斯的评论。

我手动设置了PropertyItem:

代码语言:javascript
运行
复制
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");
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45166897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档