This tutorial shows how to access metadata from an image and display it to the console in a C# Windows Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use LEADTOOLS Imaging SDK technology in a C# Windows Console application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (3 KB) |
Platform | Console C# Application |
IDE | Visual Studio 2017, 2019 |
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 - 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). For this project, the following references are needed:
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Pdf
If local DLL references are used, 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
For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.
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 Solution Explorer, open Program.cs
. Add the following statements to the using
block at the top of Program.cs
:
// Using block at the top
using System;
using System.IO;
using System.Collections.Generic;
using Leadtools;
using Leadtools.Codecs;
Add a new method called GatherMetadata
and call it in the Main
method after SetLicense()
call. Add the code below to gather and display the metadata of an image.
static void GatherMetadata()
{
string srcFile = @"C:\LEADTOOLS22\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 application runs and displays information from the sample image within the console. This tutorial uses the sample image from this file path: <INSTALL_DIR>\LEADTOOLS22\Resources\Images\cannon.jpg
This tutorial showed how to load an image and read its metadata items. Also, it covered how to use the RasterCodecs
class.