Crunch Mode Blog - A State of Mind by Developers at D2Soft Technologies

Like us on Facebook

Read metadata in digital photo files using ASP.NET and C#

Metadata is extra information stored in a digital photo that can be useful.  However, this data is not that easy to retrieve as there are many different standards to store the information.  Here is a C# code snippet that shows how to read metadata from an image file.  It reads metadata from the Exif format (Exchangeable image file format).  Note that this code is not bullet proof.  From my testing, the code correctly gets metadata from Exif version 2.21, but not from 2.20.

// Set the path to your photo file.
string file = @"c:\temp\photo.jpg"

// Get file size.
FileInfo info = new FileInfo(file);
long fileSize = info.Length;

// Get photo in memory.
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream, false, false);

// Get image size.
int width = image.Width;
int height = image.Height;

  PropertyItem item;
  string data;
  string version
  string cameraBrand;
  string cameraModel;
  DateTime dateTaken;
  DateTime dateModified;

  // Get version of Exif metadata.
  item = image.GetPropertyItem(0x9000);
  version = Encoding.UTF8.GetString(item.Value);

  // Camera brand.
  item = image.GetPropertyItem(0x010F);
  cameraBrand = Encoding.UTF8.GetString(item.Value, 0, item.Value.Length-1);

  // Camera model.
  item = image.GetPropertyItem(0x0110);
  cameraModel = Encoding.UTF8.GetString(item.Value, 0, item.Value.Length-1);

  // Date photo taken.
  item = image.GetPropertyItem(0x9003);  // Item 36867
  data = Encoding.UTF8.GetString(item.Value, 0, item.Value.Length-1);
  dateTaken = DateTime.ParseExact(data, "yyyy:MM:dd HH:mm:ss"), CultureInfo.InvariantCulture);

  // Date photo modified.
  item = image.GetPropertyItem(0x9004);
  data = Encoding.UTF8.GetString(item.Value, 0, item.Value.Length-1);
  dateModified = DateTime.ParseExact(data, "yyyy:MM:dd HH:mm:ss"), CultureInfo.InvariantCulture);
  // Error

Here are a few links that can be usefull to get more information about metadata: