Pull Metadata from an Image - C# .NET 6

This tutorial shows how to access metadata from an image and display it to the console in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to pull metadata from an image in a C# .NET 6 Console application.
Completion Time 10 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Development License Download LEADTOOLS
Try it in another language
  • C#: .NET 6+ (Console)

  • Python: Python

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 Pull Metadata from an Image - C# .NET 6 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 via NuGet packages.

This tutorial requires the following NuGet package:

For a complete list of which DLL files are required for your application, refer to Files to be Included With 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:

Gather and Display the Metadata

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.IO; 
using System.Collections.Generic; 
using Leadtools; 
using Leadtools.Codecs; 

Add a new method in the Program class, named GatherMetadata(). This method will be called inside the Main() method, below the call to the set license code. Add the code below to gather and display the metadata of the given image to the console.

C#
static void GatherMetadata() 
{ 
   string srcFile = @"C:\LEADTOOLS23\Resources\Images\cannon.jpg"; 
 
   using (var rasterCodecs = new RasterCodecs()) 
   using (var fileInfo = rasterCodecs.GetInformation(srcFile, false)) 
   { 
      Console.WriteLine("Format: {0}", fileInfo.Format); 
 
      // Check if metadata is supported for the specified file format 
      bool isMetadataItemsSupported = RasterCodecs.MetadataItemsSupported(fileInfo.Format); 
      Console.WriteLine("isMetadataItemsSupported: {0}", isMetadataItemsSupported); 
 
      if (isMetadataItemsSupported) 
      { 
         // Read metadata items 
         IDictionary<string, string> metadata = null; 
         try 
         { 
            metadata = rasterCodecs.ReadMetadataItems(srcFile, 1); 
         } 
         catch (Exception ex) 
         { 
            Console.WriteLine("Unable to read metadata: {0}", ex.Message); 
         } 
 
         if (metadata != null) 
         { 
            Console.WriteLine("Items count: {0}", metadata.Count); 
            foreach (var item in metadata) 
               // Key ex: RasterCodecs.FileMetadataKeyAuthor 
               Console.WriteLine("{0}: {1}", item.Key, item.Value); 
         } 
      } 
   } 
} 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the console appears and the application displays the metadata information from the sample image within the console.

Screenshot of image metadata displayed to console.

Wrap-up

This tutorial showed how to load an image and read its metadata. Also, it covered how to use the RasterCodecs class.

See Also

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

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