Eric Bergman-Terrell's Blog

.NET Programming Tip: How to Extract the Timestamp from a JPEG File's EXIF Metadata
October 4, 2010

In addition to the bits of an image, JPEG files have timestamps and other information embedded inside. The metadata consists of EXIF (Exchangable Image File Format) tags. Click here for EXIF specifications.

You can extract a JPEG file's timestamp as follows:

  1. Instantiate an ASCIIEncoding object.
  2. Instantiate an Image object associated with the JPEG file by calling Bitmap.FromFile. Since the Image class implements the IDisposable interface, I recommend enclosing the Image object in a using block.
  3. Iterate through each of the Image object's PropertyItem objects.
  4. If the PropertyItem's Type property is 2 and its Id property is 306, it's a timestamp.
  5. Convert the timestamp PropertyItem into a timestamp string by calling the ASCIIEncoding object's GetString method, with the PropertyItem's Value property as an argument.

The following console application uses the above technique to rename all JPEG files in a folder and subfolders to filenames based on the JPEG files' timestamps:

using System;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace RenJPEGToTS
{
    /// 
    /// Summary description for Class1.
    /// 
    class Class1
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length != 1)
            {
                Console.WriteLine("Usage:  RenJPEGToTS { folder }");
            }
            else
            {
                string folder = args[0];

                Console.WriteLine(string.Format("Renaming files in {0} and subfolders\n", folder));

                RenameFiles(folder);
            }
        }

        // Rename JPEG files in the specified folder (and subfolders) to filenames 
        // based on their JPEG timestamps.
        private static void RenameFiles(string Folder)
        {
            Encoding enc = new ASCIIEncoding();

            // For each JPEG file in the folder...
            foreach (string oldFilePath in Directory.GetFiles(Folder, "*.jpg"))
            {
                string newFilePath = null;

                // Create an Image object corresponding to the JPEG file.
                using (Image img = Bitmap.FromFile(oldFilePath))
                {
                    // Extract all PropertyItems from the Image.
                    foreach (PropertyItem pi in img.PropertyItems)
                    {
                        // If the PropertyItem is a timestamp...
                        if (pi.Type == 2 /* text */ && pi.Id == 306 /* date and time */)
                        {
                            // Create a filename based on the timestamp.
                            string newFileName = enc.GetString(pi.Value).Replace(" ", "_").Replace(":", "_");
                            newFilePath = Path.GetDirectoryName(oldFilePath) + @"\" + newFileName.Substring(0, newFileName.Length - 1) + ".jpg";
                        }
                    }
                }

                if (newFilePath != null && newFilePath != oldFilePath)
                {
                    Console.WriteLine(string.Format("Renaming {0} to {1}", oldFilePath, newFilePath));

                    // Rename the file.
                    File.Move(oldFilePath, newFilePath);
                }
            }

            // Rename JPEG files in each subfolder.
            foreach (string folderPath in Directory.GetDirectories(Folder))
            {
                RenameFiles(folderPath);
            }
        }
    }
}
Keywords: JPEG, EXIF, Metadata, ASCIIEncoding

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015