avr. 16

Recently, I was needed to order all the pictures that where taken during an event, but by different photographers. In Windows Explorer you can simply add a column "Date Taken" but how can we do the same by code ?

private static DateTime GetDateTaken(string imagePath)
{
   using ( Image myImage = Image.FromFile(imagePath) )
   {
      PropertyItem propItem = myImage.GetPropertyItem(0x9003); // Property "Date Taken"

      //Convert date taken metadata to a DateTime object
      string sdate = Encoding.UTF8.GetString(propItem.Value).Trim();
      return DateTime.ParseExact(sdate, "yyyy:MM:dd HH:mm:ss\0", null);
   }
}

And here it is ! As simple as that !

And if you want to have more information about the different property items of an image, just go to http://msdn.microsoft.com/en-us/library/ms534413

An so if you want to order the images by date it was taken, you could use a simple LINQ query : 

 

string path = @"C:\Temp\MyPhotos";
string[] files = Directory.GetFiles(path, "*.jpg")
                          .OrderBy(file => GetDateTaken(file)).ToArray();

 

Commentaires

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading