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
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021