Read and Write Exif Tags and Comments - Console C#

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 extract PDF attachments and convert them to PNG files 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 2017, 2019
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET Framework (Console)
  • Apple: macOS

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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:

For a complete list of which DLL files are required for your application, refer to Files to be Included in your Application.

Set the License File

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:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Read/Write Exif Comments Code

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:

C#
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.

C#
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.

C#
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(); 
        comment.Type = RasterCommentMetadataType.UserComment; 
        string comments = "The image has been flipped"; 
        char[] data = comments.ToCharArray(0, comments.Length); 
        byte[] dataComments = new byte[8 + comments.Length]; 
 
        // When you need to write a user comment the first 8 bytes must contain the "Ascii" word.  
        dataComments[0] = 41; 
        dataComments[1] = 53; 
        dataComments[2] = 43; 
        dataComments[3] = 49; 
        dataComments[4] = 49; 
        dataComments[5] = 0; 
        dataComments[6] = 0; 
        dataComments[7] = 0; 
 
        for (int i = 0; i < comments.Length; i++) 
            dataComments[8 + i] = (byte)data[i]; 
 
        // Set the data property in the comment object to the dataComments array.  
        comment.FromByte(dataComments); 
        // 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 
        comment = _codecs.ReadComment(_destFile, 1, RasterCommentMetadataType.UserComment); 
        dataComments = comment.GetData(); 
        string msg = "User comments = "; 
        for (int i = 0; i < (dataComments.Length - 8); i++) 
            msg += (char)dataComments[i + 8]; 
 
        Console.WriteLine($"{msg}"); 
    } 
} 

Add the Read/Write Exif Tags Code

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 read the tags from the designated file.

C#
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; 
    } 
 
    tags = new List<RasterTagMetadata>(_codecs.ReadTags(_srcFile, 1)); 
 
    string txtFileName = "_metadata.txt"; 
 
    using (StreamWriter writer = File.CreateText(txtFileName)) 
    { 
        // Write the tags 
        WriteTags(writer, "Tags", tags); 
    } 
 
    // Show the text file we created 
    System.Diagnostics.Process.Start(txtFileName); 
} 

Add a new method to the Program class named WriteTags(StreamWriter writer, string name, List<RasterTagMetadata> tags). Call this method inside the ReadAndWriteExifTags method, as shown above. Add the code below to the WriteTags method to write the tags read to a TXT file.

C#
static void WriteTags(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

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.

Wrap-up

This tutorial showed how to read and write Exif comments and tags. Also, we covered how to use the RasterCodecs and RasterTagMetadata classes.

See Also

Help Version 22.0.2021.10.18
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.