Extract Image Information - C# .NET 6

This tutorial shows how to gather information from various image files supported by LEADTOOLS using the CodecsImageInfo class in a C# .NET 6 Console application.

Overview  
Summary This tutorial covers how to use the CodecsImageInfo class in a C# .NET 6 application.
Completion Time 15 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

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 this 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 the project is not available, 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:

Add the Get Image Information 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.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.

C#
static void Main(string[] args) 
{ 
    InitLEAD(); 
 
    RasterCodecsImageInfo(@"C:\LEADTOOLS23\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.

C#
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 implements IDisposable, make sure it is in a using statement for proper disposal.

Handling Streams

If you want to load the image using memory stream, then add the following code in the RasterCodecsImageInfo(string fileName) method instead of the CodecsImageInfo info = codecs.GetInformation(fileName, true) statement:

C#
CodecsImageInfo info; 
byte[] fileData = File.ReadAllBytes(fileName); 
using (MemoryStream fileStream = new MemoryStream(fileData)) 
   info = codecs.GetInformation(fileStream, true); 

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 gathers the image information and outputs it to the console.

The application runs and the console displays the extracted information.

Wrap-up

This tutorial showed how to gather information from an image file path using the CodecsImageInfo class.

See Also

Help Version 23.0.2024.4.23
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.