从位图中获取 EXIF 属性 中的拍摄日期

Get Taken Date in EXIF property from Bitmap

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

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 拍摄的日期吗?

提前致谢。

最后我在 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") + "[=10=]");
     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");
}