Modify EXIF metadata using C#
EXIF (Exchangeable Image File Format) is a standard used by digital cameras that specifies metadata tags such as camera settings, image metrics, date and time information, and more.
Supported image formats
- JPEG (read and write)
- PNG (read and write)
- RAW (read only)
- TIFF (read and write)
- WEBP (read and write)
GPS tags
GPS tags are used by digital cameras that contain positioning information. They’re specified by the same standard as EXIF tags. This means that all methods described below are also applicable for GPS tags, but only for the following image formats:
- JPEG (read and write)
- TIFF (read and write)
Getting EXIF tags
To retrieve EXIF tag data, use the following methods:
All the methods listed above require the image ID and the EXIF tag number.
The tag number may be different for every EXIF tag. This means that to get the exact EXIF tag you want, you have to loop through all tags. To get the total number of EXIF tags, use the TagCount
method.
The code below shows how to list the EXIF tags of an image file:
using GdPictureImaging gdpictureImaging = new GdPictureImaging();int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Create a `StringBuilder` object to store tags.StringBuilder report = new StringBuilder();// Get tag count.int tagCount = gdpictureImaging.TagCount(imageID);for (int i = 1; i <= tagCount; i++){ // Get tag ID. Tags tagID = gdpictureImaging.TagGetID(imageID, i); //Get tag type. TagType tagType = gdpictureImaging.TagGetType(imageID, i); // Get tag name. string tagName = gdpictureImaging.TagGetName(imageID, i); // Get tag value. string tagValue = gdpictureImaging.TagGetValueString(imageID, i); // Append tag data. report.AppendLine($"{tagID} {tagType} {tagName} {tagValue}");}Console.WriteLine($"{report}");GdPictureDocumentUtilities.DisposeImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Create a `StringBuilder` object to store tags. Dim report As StringBuilder = New StringBuilder() ' Get tag count. Dim tagCount As Integer = gdpictureImaging.TagCount(imageID)
For i = 1 To tagCount ' Get tag ID. Dim tagID As Tags = gdpictureImaging.TagGetID(imageID, i) ' Get tag type. Dim tagType As TagType = gdpictureImaging.TagGetType(imageID, i) ' Get tag name. Dim tagName As String = gdpictureImaging.TagGetName(imageID, i) ' Get tag value. Dim tagValue As String = gdpictureImaging.TagGetValueString(imageID, i) ' Append tag data. report.AppendLine($"{tagID} {tagType} {tagName} {tagValue}") Next
Console.WriteLine($"{report}") GdPictureDocumentUtilities.DisposeImage(imageID)End Using
Related topics
Setting an EXIF tag
To set an EXIF tag, use one of the following methods:
TagSetValueString
— For tags containing a string value.TagSetValueByte
— For tags containing a byte value.
Both methods require the following parameters:
imageID
— Image ID.TagID
— Tag ID, which is a member of theTags
enumeration.TagType
— Tag type, which is a member of theTagType
enumeration.TagData
— The new value of the tag. It can be either a string or a byte value, depending on which method you use.
The code below shows how to modify an EXIF tag of an image file that specifies the date on which the image was taken:
using GdPictureImaging gdPictureImaging = new GdPictureImaging();int imageID = gdPictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");// Get tag count.int tagCount = gdPictureImaging.TagCount(imageID);gdPictureImaging.TagSetValueString(imageID, Tags.TagExifDTOrig, TagType.TagTypeASCII, "2020:11:30 13:45:29");// Save the image in a new file.gdPictureImaging.SaveAsJPEG(imageID, @"C:\temp\output.jpg");gdPictureImaging.ReleaseGdPictureImage(imageID);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() Dim imageID As Integer = gdPictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg") ' Get tag count. Dim tagCount As Integer = gdPictureImaging.TagCount(imageID) gdPictureImaging.TagSetValueString(imageID, Tags.TagExifDTOrig, TagType.TagTypeASCII, "2020:11:30 13:45:29") ' Save the image in a new file. gdPictureImaging.SaveAsJPEG(imageID, "C:\temp\output.jpg") gdPictureImaging.ReleaseGdPictureImage(imageID)End Using
Related topics