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 |
|
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.
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:
Leadtools.Formats.Raster.Common
For a complete list of which DLL files are required for your application, refer to Files to be Included With 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.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.
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 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.
This tutorial showed how to load an image and read its metadata. Also, it covered how to use the RasterCodecs
class.