Edit IPTC metadata using C#

IPTC

IPTC is an extension of the EXIF standard, and it contains additional information not included in EXIF tags. This information refers to the image content to help manage and categorize images and includes the image category, copyright notice, and more. Custom IPTC tags can also be created.

Supported image formats

  • JPEG (read and write)
  • TIFF (read and write)

Getting IPTC tags

To retrieve IPTC tag data, use the following methods:

All methods listed above require the image ID and the IPTC tag number.

The tag number may be different for every IPTC tag. This means that to get the exact IPTC tag you want, you have to loop through all tags. To get the total number of IPTC tags, use the IPTCCount method.

The code below shows how to list IPTC 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.IPTCCount(imageID);
for (int i = 1; i <= tagCount; i++)
{
// Get tag ID.
IPTCTags tagID = gdpictureImaging.IPTCGetID(imageID, i);
//Get tag type.
TagType tagType = gdpictureImaging.IPTCGetType(imageID, i);
// Get tag value.
string tagValue = gdpictureImaging.IPTCGetValueString(imageID, i);
// Append tag data.
report.AppendLine($"{tagID} {tagType} {tagValue}");
}
Console.WriteLine($"{report}");
GdPictureDocumentUtilities.DisposeImage(imageID);

Setting an IPTC tag

To set an IPTC tag, use one of the IPTCSetValueString method. It requires the following parameters:

  • imageID — Image ID.
  • IPTCTagID — IPTC tag ID, which is a member of the IPTCTags enumeration.
  • TagData — The new string value of the tag.

The code below shows how to modify an IPTC tag of an image file that specifies the location of the city the image was taken in:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Set the value of the `IPTCTagCity` tag.
gdpictureImaging.IPTCSetValueString(imageID, IPTCTags.IPTCTagCity, "New York");
gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\source.jpg");
GdPictureDocumentUtilities.DisposeImage(imageID);

Deleting an IPTC tag

To delete an IPTC tag, use the IPTCDelete method. It requires the following parameters:

  • imageID — Image ID.
  • TagNo — IPTC tag number attached to the image.

The tag number may be different for every IPTC tag. This means that to get the exact IPTC tag you want, you have to loop through all tags. To get the total number of IPTC tags, use the IPTCCount method:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Get tag count.
int tagCount = gdpictureImaging.IPTCCount(imageID);
for (int i = 1; i <= tagCount; i++)
{
// If the current tag is `IPTCTagCity`, then delete the tag.
if(gdpictureImaging.IPTCGetID(imageID, i) == IPTCTags.IPTCTagCity)
gdpictureImaging.IPTCDelete(imageID, i);
}
gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\source.jpg");
GdPictureDocumentUtilities.DisposeImage(imageID);

Creating a Custom IPTC tag

To create a custom IPTC tag, you have to add a new member to the IPTCTags enumeration and assign a value to it:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
// Create a `StringBuilder` object to store tag data.
StringBuilder report = new StringBuilder();
// Get tag count.
int tagCount = gdpictureImaging.IPTCCount(imageID);
// Add an empty custom tag to the `IPTCTags` enumeration.
IPTCTags customTag = (IPTCTags)(tagCount + 1);
// Set a value to the custom tag.
gdpictureImaging.IPTCSetValueString(imageID, customTag, "Custom tag value");
for (int i = 1; i <= tagCount + 1; i++)
{
// Get tag ID.
IPTCTags tagID = gdpictureImaging.IPTCGetID(imageID, i);
// Get tag type.
TagType tagType = gdpictureImaging.IPTCGetType(imageID, i);
// Get tag value.
string tagValue = gdpictureImaging.IPTCGetValueString(imageID, i);
// Append tag data.
report.AppendLine($"{tagID} {tagType} {tagValue}");
}
gdpictureImaging.SaveAsJPEG(imageID, @"C:\temp\source.jpg");
Console.WriteLine($"{report}");
GdPictureDocumentUtilities.DisposeImage(imageID);