This is a non-iMIS problem I wanted to solve for my photography hobby, but there may be some interest in how to use LINQ to work with EXIF data from image files.
In this case, I had photos from two different camera bodies and the same event, and wanted to post them to a photo site, ordered by the date/time they were taken. Unfortunately, the site didn't support sort by date taken, just by file date modified. Since the photo ID numbers for the cameras weren't in sync, I couldn't use them for the sort. I'd started with .NEF format image files, and generated .JPEGs from them, so the order of the file date modified didn't necessarily match up with the date taken.
I found the EXIF spec here: http://www.exif.org/Exif2-2.PDF and located the tag for DateTimeOriginal (36867). Then I wrote a console utility to set the file date created and modified dates to the value of DateTimeOriginal. The code that does the work follows.
// given an input directory path, set the file create and modify dates for image files to the EXIF date taken
static void SetFileDatesToEXIFDateTaken (string inPath)
{
DateTime dateTaken;
string sdate;
string datepart;
string timepart;
Image image;
PropertyItem[] propItems;
string[] filePaths = Directory.GetFiles (inPath);
int countProcessedFiles = 0;
foreach (string filePath in filePaths)
{
Console.WriteLine(filePath);
try
{
image = new Bitmap(filePath); // create image object, to get the PropertyItems
propItems = image.PropertyItems; // get the PropertyItems from the image, where the EXIF tags are stored
image.Dispose(); // dispose of the image, so the date can be updated later (avoiding "file in use" errors)
// instead of iterating through every propItem in propItems, just get the DateTimeOriginal tag's value (ID 36867, hex 9003)
var queryID36867 = from PropertyItem propItem in propItems
where propItem.Id == 36867
select propItem.Value;
foreach (var s in queryID36867)
{
//Convert date taken metadata into a DateTime object
sdate = Encoding.UTF8.GetString(s).Trim();
timepart = sdate.Substring(sdate.IndexOf(" "), (sdate.Length - sdate.IndexOf(" ")));
datepart = sdate.Substring(0, 10);
datepart = datepart.Replace(":", "-");
sdate = datepart + timepart;
dateTaken = DateTime.Parse(sdate);
try
{
File.SetCreationTime(filePath, dateTaken);
File.SetLastWriteTime(filePath, dateTaken);
++countProcessedFiles;
}
catch (IOException e)
{
Console.WriteLine(e.Message);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
continue; // couldn't create an Image, so try the next file
}
}
Console.WriteLine();
Console.WriteLine("Finished - {0} files updated.", countProcessedFiles.ToString());
}
Photo links:
http://hueyphoto.com
http://www.flickr.com/photos/henry_huey
http://www.flickr.com/photos/dragonbandphotos