1

I have some pictures with date created set to when I unzipped them and date modified set to some strange moment in the past. However, the 'Date' attribute shows a coorect creation time (the time when I actually took these pictures). Is there a way to access that attribute in C# ? Both File.GetCreationTime and FileInfo.CreationTime give me the incorrect 'Date created'. enter image description here

This is what I tried:

var allFiles = Directory.EnumerateFiles(".");
foreach (var s in allFiles)
{
    Console.WriteLine(s + " " + File.GetCreationTime(s));
}

DirectoryInfo dir = new DirectoryInfo(".");
FileInfo[] files = dir.GetFiles().OrderByDescending(p => p.CreationTime).ToArray();

foreach (var f in files)
{
    Console.WriteLine(f.Name + " " + f.CreationTime + "/" + f.LastAccessTime + "/" + f.LastWriteTime);
}

EDIT:

This 'Date' is actually a 'Date taken' field and it's not a file attribute but a part of metadata of the image files.

Maciej Szpakowski
  • 571
  • 1
  • 7
  • 22

2 Answers2

2

Try this:

public DateTime? GetDateTakenFromBitmap(string bitmapFileName)
{
    using (var bm = System.Drawing.Bitmap.FromFile(bitmapFileName))
    {
        return
            bm
                .PropertyItems
                .Where(x => x.Id == 0x9003)
                .Select(x =>
                {
                    DateTime dt;
                    var enc = new ASCIIEncoding();
                    var parsed = DateTime.TryParseExact(
                        enc.GetString(x.Value, 0, x.Len - 1),
                        "yyyy:MM:dd HH:mm:ss",
                        CultureInfo.CurrentCulture,
                        DateTimeStyles.None,
                        out dt);
                    return parsed ? (DateTime?)dt : null;
                })
                .FirstOrDefault();
    }
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

Yes you can. These kinds of meta data are known as EXIF tags in a JPG file.

Basically you need to load the JPG into a System.Drawing.Bitmap, then enumerate the bitmap's PropertyItems collection looking for id 0x9003, which correlates to original date. When you find that Id, the corresponding Value will be a string representation of the original date.

You can search for EXIF tags to list all of the available IDs.

Tim
  • 5,940
  • 1
  • 12
  • 18