This tutorial shows how to gather information from various image files supported by LEADTOOLS using the CodecsImageInfo class in a C# .NET Core application.
Overview | |
---|---|
Summary | This tutorial covers how to use the CodecsImageInfo class in a C# .NET Core application. |
Completion Time | 30 minutes |
Visual Studio Project | Download tutorial project (1 KB) |
Platform | C# .NET Core Console 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 Extract Image Information - C# .NET Core 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:
Note
Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.
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 Leadtools;
using Leadtools.Codecs;
Add a new method in the Program class, named RasterCodecsImageInfo(string fileName)
. This method will be called inside the Main()
method below the set license code, as shown below.
static void Main(string[] args)
{
if (!SetLicense())
Console.WriteLine("Error setting license");
else
Console.WriteLine("License file set successfully");
RasterCodecsImageInfo(@"C:\LEADTOOLS21\Resources\Images\image1.cmp");
}
Add the code below to the new method to gather the image information from the given file path and output it to the console.
static void RasterCodecsImageInfo(string fileName)
{
using (RasterCodecs codecs = new RasterCodecs())
{
CodecsImageInfo info = codecs.GetInformation(fileName, true);
string inputFileName = Path.GetFileNameWithoutExtension(fileName);
string codecsInfoString = ($"Image Format: {info.Format}\n" +
$"Information for: {inputFileName}\n" +
$"BitsPerPixel: {info.BitsPerPixel}\n" +
$"BytesPerLine: {info.BytesPerLine}\n" +
$"ColorSpace: {info.ColorSpace}\n" +
$"Byte Order: {info.Order}\n" +
$"Image Height: {info.Height}\n" +
$"Image Width: {info.Width}\n" +
$"Image X Resolution: {info.XResolution}\n" +
$"Image Y Resolution: {info.YResolution}\n" +
$"Compression: {info.Compression}\n" +
$"Page Number: {info.PageNumber}\n" +
$"Total Pages: {info.TotalPages}");
Console.WriteLine(codecsInfoString);
}
}
Note
There are more properties inside the
CodecsImageInfo
class. The snippet above showcases the most commonly used properties.Because the
RasterCodecs
class implementsIDisposable
, make sure it is in ausing
statement for proper disposal.
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the console appears and the application gathers the image information and outputs it to the console.
This tutorial showed how to gather information from an image file path using the CodecsImageInfo
class.