Extract the timestamp from a JPEG file's EXIF metadata in C#

.NET programming topics
Post Reply
Cyclops
Lieutenant
Lieutenant
Posts: 71
Joined: Wed Jul 15, 2009 1:48 pm
Location: London

Extract the timestamp from a JPEG file's EXIF metadata in C#

Post by Cyclops » Thu Dec 31, 2009 9:41 am

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.

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:

Code: Select all

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);
            }
        }
    }
}
Post Reply

Return to “.NET Programming”