0

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.

  • 2
    filename.Split('_')[1] theres the date haha, but more serious if you save it yourself and go to properties is the date correct or no date? – EpicKip Jul 18 '17 at 12:33
  • 2
    `GetPropertyItem(36867)` gets it I'd wager [`SetPropertyItem()`](https://msdn.microsoft.com/en-us/library/system.drawing.image.setpropertyitem(v=vs.110).aspx) sets it. – Alex K. Jul 18 '17 at 12:35
  • You are right @EpicKip, instead of returning null, I can return new FileInfo(path).LastWriteTime.. The problem still persists if I want to move photos in another place. No? – Simon Bross Jul 18 '17 at 14:02
  • @SimonBross That was a joke suggestion bud, look at what Alex is saying – EpicKip Jul 18 '17 at 14:04

1 Answers1

2

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