This tutorial shows how to read and write Exif tags and comments in a C# Windows Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to work with Exif tags and comments in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2019, 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Read and Write Exif Tags and Comments - Console C# tutorial.
Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.
The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
If using local DLL references, the following DLLs are needed.
The DLLs are located at <INSTALL_DIR>\LEADTOOLS22\Bin\Dotnet4\x64
:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Tif.dll
For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.
The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.
There are two types of runtime licenses:
With the project created, the references added, and the license set, coding can begin.
In the Solution Explorer, open Program.cs
. Add the following statements to the using block at the top of Program.cs
:
using System;
using System.Collections.Generic;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
using Leadtools.ImageProcessing;
Add two new string variables to the Main
method named _srcFile
and _destFile
above the call to the SetLicense
method. Set _srcFile
equal to the file path containing the source EXIF file and set _destFile
equal to the file path to export the EXIF file containing the added comment.
Add a new method to the Program
class named ReadAndWriteExifComments(string _srcFile, string _destFile, RasterCodecs _codecs)
. Call this method below the call to the SetLicense
method, inside a using
statement initiating a new instance of RasterCodecs
, as shown below.
static void Main(string[] args)
{
string _srcFile = @"FILE PATH TO SOURCE EXIF FILE";
string _destFile = @"FILE PATH TO OUTPUF EXIF FILE";
SetLicense();
using (RasterCodecs _codecs = new RasterCodecs())
{
ReadAndWriteExifComments(_srcFile, _destFile, _codecs);
ReadAndWriteExifTags(_srcFile, _codecs);
}
Console.WriteLine("Press any key to exit");
Console.ReadKey(true);
}
Add the below code to the ReadAndWriteExifComments
method to flip the loaded image, write a tag to the file stating that the image has been flipped, and then read the user comment created.
static void ReadAndWriteExifComments(string _srcFile, string _destFile, RasterCodecs _codecs)
{
using (RasterImage _image = _codecs.Load(_srcFile))
{
// Write Comment
// Flip the image.
FlipCommand flipCmd = new FlipCommand(true);
flipCmd.Run(_image);
// Add a user comment to the file and save it in another name.
RasterCommentMetadata comment = new RasterCommentMetadata();
// When writing a user comment, the first 8 bytes must contain the "ASCII" word followed by 3 null characters.
comment.Type = RasterCommentMetadataType.UserComment;
string commentString = "ASCII\0\0\0";
commentString += "The image has been flipped.";
// Convert the string to byte array and set the bytes into the comment
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(commentString);
comment.FromByte(bytes);
// Add the user comment to the comments collection of the image
_image.Comments.Add(comment);
_codecs.Options.Save.Comments = true;
_codecs.Save(_image, _destFile, RasterImageFormat.Exif, 24);
// Read the comments
RasterCommentMetadata commentRead = _codecs.ReadComment(_destFile, 1, RasterCommentMetadataType.UserComment);
byte[] commentBytes = commentRead.GetData();
string commentStringRead = System.Text.Encoding.ASCII.GetString(commentBytes, 8, commentBytes.Length - 8);
Console.WriteLine($"User comments = {commentStringRead}");
}
}
Add a new method to the Program
class named ReadAndWriteExifTags(string _srcFile, RasterCodecs _codecs)
. Call this method below the call to the ReadAndWriteExifComments
method, inside a using
statement initiating a new instance of RasterCodecs
, as shown in the previous section. Add the code below to the ReadAndWriteExifTags
method to write a private tag then read all the tags from the designated file.
static void ReadAndWriteExifTags(string _srcFile, RasterCodecs _codecs)
{
RasterImageFormat format;
using (CodecsImageInfo info = _codecs.GetInformation(_srcFile, false))
{
format = info.Format;
}
// Load the tags
List<RasterTagMetadata> tags = null;
if (!RasterCodecs.TagsSupported(format))
{
Console.WriteLine("File Format Does Not Support Tags");
return;
}
int PhoneNumber = 0x8001; // Add a private (custom) "Phone Number" tag
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("+1-800-637-4699");
RasterTagMetadata tag = new RasterTagMetadata(PhoneNumber, RasterTagMetadataDataType.Ascii, bytes);
_codecs.WriteTag(_srcFile, 1, tag);
tags = new List<RasterTagMetadata>(_codecs.ReadTags(_srcFile, 1));
string txtFileName = "_metadata.txt";
using (StreamWriter writer = File.CreateText(txtFileName))
{
// List the tags into a text file
ExportTags(writer, "Tags", tags);
}
// Show the text file we created
System.Diagnostics.Process.Start(txtFileName);
}
Add a new method to the Program
class named ExportTags(StreamWriter writer, string name, List<RasterTagMetadata> tags)
. Call this method inside the ReadAndWriteExifTags
method, as shown above. Add the code below to the ExportTags
method to list the tags read to a TXT file.
static void ExportTags(StreamWriter writer, string name, List<RasterTagMetadata> tags)
{
writer.WriteLine("{0}:", name);
if (tags != null)
{
foreach (RasterTagMetadata tag in tags)
{
writer.WriteLine("Id: 0x{0}, data length: {1}", tag.Id.ToString("X"), tag.GetData().Length);
}
}
else
{
writer.WriteLine("Not supported");
}
writer.WriteLine();
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application runs and initially writes an Exif user comment, reads the written user comment, then reads the Exif tags inside the given image and writes those tags to an external TXT file.
This tutorial showed how to read and write Exif comments and tags. Also, we covered how to use the RasterCodecs
and RasterTagMetadata
classes.