Simon Bross
Simon Bross

Reputation: 31

Get Taken Date in EXIF property from Bitmap

I have an application which analyses images. I have to retreive the Date Taken of an image. I use this function :

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;
    }
}

My application worked well with photos taken by a Camera. But now, I save photos from a Webcam like this :

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");
    }
}

In this case, my previous function does not work because the image file does not contain any PropertyItem.

Is there any way to retreive the date taken by the PropertyItem when we save the image manually?

Thanks in advance.

Upvotes: 0

Views: 1340

Answers (1)

Simon Bross
Simon Bross

Reputation: 31

Finally I found a solution with the comment of Alex.

I set the PropertyItem manually :

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");
}

Upvotes: 2

Related Questions