Working with EXIF metadata

What is EXIF?

According to the specification, EXIF (Exchangeable image file format) is a standard that specifies the formats to be used for images, sound and tags in digital still cameras and in other systems handling the image and sound files recorded by digital still cameras. Despite the confusing definition and name of the format, EXIF is just a metadata standard. In fact, it simply defines a way to store metadata properties in a variety of well-known image and audio formats. The EXIF tag structure is borrowed from TIFF files. The specification declares a set of tags intended to store technical details such as the geolocation of the place where a picture was taken, the name of the camera owner, camera settings, etc. 

Note
Please refer to the following article to get more information on the standard.

Reading basic EXIF properties

To access EXIF metadata in a file of any supported format, GroupDocs.Metadata provides the IExif.ExifPackage property. The following are the steps to read EXIF metadata:

  1. Load a file that contains EXIF metadata
  2. Extract the EXIF metadata package using the IExif.ExifPackage property

The following code snippet gets EXIF properties of a TIFF image and displays them on the screen. 

AdvancedUsage.WorkingWithMetadataStandards.Exif.ReadBasicExifProperties

using (Metadata metadata = new Metadata(Constants.TiffWithExif))
{
	IExif root = metadata.GetRootPackage() as IExif;
	if (root != null && root.ExifPackage != null)
	{
		Console.WriteLine(root.ExifPackage.Artist);
		Console.WriteLine(root.ExifPackage.Copyright);
		Console.WriteLine(root.ExifPackage.ImageDescription);
		Console.WriteLine(root.ExifPackage.Make);
		Console.WriteLine(root.ExifPackage.Model);
		Console.WriteLine(root.ExifPackage.Software);
		Console.WriteLine(root.ExifPackage.ImageWidth);
		Console.WriteLine(root.ExifPackage.ImageLength);

		// ...

		Console.WriteLine(root.ExifPackage.ExifIfdPackage.BodySerialNumber);
		Console.WriteLine(root.ExifPackage.ExifIfdPackage.CameraOwnerName);
		Console.WriteLine(root.ExifPackage.ExifIfdPackage.UserComment);

		// ...

		Console.WriteLine(root.ExifPackage.GpsPackage.Altitude);
		Console.WriteLine(root.ExifPackage.GpsPackage.LatitudeRef);
		Console.WriteLine(root.ExifPackage.GpsPackage.LongitudeRef);

		// ...
	}
}

Reading all EXIF tags

In some cases, it’s necessary to read all EXIF properties from a file, including custom ones. To achieve this the GroupDocs.Metadata API provides direct access to the EXIF tags extracted from a file.

  1. Load a file that contains EXIF metadata
  2. Extract the EXIF metadata package using the IExif.ExifPackage property
  3. Iterate through all EXIF tags on different levels

AdvancedUsage.WorkingWithMetadataStandards.Exif.ReadExifTags

using (Metadata metadata = new Metadata(Constants.JpegWithExif))
{
	IExif root = metadata.GetRootPackage() as IExif;
	if (root != null && root.ExifPackage != null)
	{
		const string pattern = "{0} = {1}";

		foreach (TiffTag tag in root.ExifPackage.ToList())
		{
			Console.WriteLine(pattern, tag.TagID, tag.Value);
		}

		foreach (TiffTag tag in root.ExifPackage.ExifIfdPackage.ToList())
		{
			Console.WriteLine(pattern, tag.TagID, tag.Value);
		}

		foreach (TiffTag tag in root.ExifPackage.GpsPackage.ToList())
		{
			Console.WriteLine(pattern, tag.TagID, tag.Value);
		}
	}
}

Reading a specific EXIF tag

The GroupDocs.Metadata API also supports reading specific EXIF tags using an indexer. Follow below-mentioned steps to read a specific EXIF tag.

  1. Load a file that contains EXIF metadata
  2. Extract the EXIF metadata package using the IExif.ExifPackage property
  3. Get a specific tag using the ExifPackage class indexer

AdvancedUsage.WorkingWithMetadataStandards.Exif.ReadSpecificExifTag

using (Metadata metadata = new Metadata(Constants.TiffWithExif))
{
    IExif root = metadata.GetRootPackage() as IExif;
    if (root != null && root.ExifPackage != null)
    {
        TiffAsciiTag software = (TiffAsciiTag)root.ExifPackage[TiffTagID.Software];
        if (software != null)
        {
            Console.WriteLine("Software: {0}", software.Value);
        }
 
        TiffUndefinedTag comment = (TiffUndefinedTag)root.ExifPackage.ExifIfdPackage[TiffTagID.UserComment];
        if (comment != null)
        {
            Console.WriteLine("Comment: {0}", comment.InterpretedValue);
        }
    }
}

Updating EXIF properties

The GroupDocs.Metadata API facilitates the user to update EXIF metadata in a convenient way - using the ExifPackage class properties. Follow the below steps to update EXIF metadata in a file of any supported format.

  1. Load a file that contains EXIF metadata
  2. Extract the EXIF metadata package using the IExif.ExifPackage property
  3. Assign values to desired EXIF properties
  4. Save the changes

AdvancedUsage.WorkingWithMetadataStandards.Exif.UpdateExifProperties

using (Metadata metadata = new Metadata(Constants.InputJpeg))
{
	IExif root = metadata.GetRootPackage() as IExif;
	if (root != null)
	{
		// Set the EXIF package if it's missing
		if (root.ExifPackage == null)
		{
			root.ExifPackage = new ExifPackage();
		}

		root.ExifPackage.Copyright = "Copyright (C) 2011-2019 GroupDocs. All Rights Reserved.";
		root.ExifPackage.ImageDescription = "test image";
		root.ExifPackage.Software = "GroupDocs.Metadata";

		// ...

		root.ExifPackage.ExifIfdPackage.BodySerialNumber = "test";
		root.ExifPackage.ExifIfdPackage.CameraOwnerName = "GroupDocs";
		root.ExifPackage.ExifIfdPackage.UserComment = "test comment";

		// ...

		metadata.Save(Constants.OutputJpeg);
	}
}

Adding or updating custom EXIF tags

The GroupDocs.Metadata API allows adding or updating custom tags in an EXIF package.

  1. Load a file that contains EXIF metadata
  2. Extract the EXIF metadata package using the IExif.ExifPackage property
  3. Set the EXIF package if it’s missing
  4. Add any number of custom tags to the package
  5. Save the changes

AdvancedUsage.WorkingWithMetadataStandards.Exif.SetCustomExifTag

using (Metadata metadata = new Metadata(Constants.TiffWithExif))
{
	IExif root = metadata.GetRootPackage() as IExif;
	if (root != null)
	{
		// Set the EXIF package if it's missing
		if (root.ExifPackage == null)
		{
			root.ExifPackage = new ExifPackage();
		}

		// Add a known property
		root.ExifPackage.Set(new TiffAsciiTag(TiffTagID.Artist, "test artist"));

		// Add a fully custom property (which is not described in the EXIF specification).
		// Please note that the chosen ID may intersect with the IDs used by some third party tools.
		root.ExifPackage.Set(new TiffAsciiTag((TiffTagID)65523, "custom"));

		metadata.Save(Constants.OutputTiff);
	}
}

Here is a full list of tags that can be added to an EXIF package:

Removing EXIF metadata

To remove the EXIF package from a file just assign null to the IExif.ExifPackage property. The code sample below shows how to remove EXIF metadata from a file.

AdvancedUsage.WorkingWithMetadataStandards.Exif.RemoveExifMetadata

using (Metadata metadata = new Metadata(Constants.JpegWithExif))
{
	IExif root = metadata.GetRootPackage() as IExif;
	if (root != null)
	{
		root.ExifPackage = null;
		metadata.Save(Constants.OutputJpeg);
	}
}

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

Along with full featured .NET library we provide simple, but powerful free Apps. You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.