Detect Image Format and Extension - C# .NET 6

This tutorial shows how to read a given image file and detect the proper format, along with the appropriate file extension in a C# .NET 6 application using the LEADTOOLS SDK. This is useful if the file does not have a file extension or if you are reading the file from a stream.

Overview  
Summary This tutorial covers how to use the RasterCodecs class to detect a file's format and file extension in a C# Windows Console application.
Completion Time 20 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 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).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

For a complete list of which DLL files are required for your application, refer to Files to be Included in 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 Detect Image Format 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 the code below to the Main() method to load the image from the given file and state the file extension. For the purposes of this tutorial the TIFF image in the following file path was used: C:\LEADTOOLS23\Resources\Images\ocr1.tif

C#
static void Main(string[] args) 
{ 
   try 
   { 
      string _inputFile = @"C:\LEADTOOLS23\Resources\Images\ocr1.tif"; 
 
      InitLEAD(); 
 
      // Retrieve information on the file without fully loading it 
      using RasterCodecs _codecs = new RasterCodecs(); 
      using CodecsImageInfo _info = _codecs.GetInformation(_inputFile, false); 
      // Get the proper RasterImageFormat and the friendly name for it 
      RasterImageFormat format = _info.Format; 
      string extension = RasterCodecs.GetExtension(format); 
      Console.WriteLine($"This file has the extension {extension}"); 
      /* 
      // Retrieve information on the file without fully loading it using Memory stream 
      using RasterCodecs _codecs = new RasterCodecs(); 
 
      byte[] data = File.ReadAllBytes(_inputFile); 
      using MemoryStream ms = new MemoryStream(data); 
      using CodecsImageInfo _info = _codecs.GetInformation(ms, false); 
 
      // Get the proper RasterImageFormat and the friendly name for it 
      RasterImageFormat format = _info.Format; 
      string extension = RasterCodecs.GetExtension(format); 
      Console.WriteLine($"This file has the extension {extension}"); 
      */ 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.Message); 
      Console.WriteLine(ex.StackTrace); 
      Console.ReadKey(true); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

Handling Streams

To handle this using MemoryStream replace the code under the using statements in the Main() method with the following.

C#
using RasterCodecs _codecs = new RasterCodecs(); 
byte[] data = File.ReadAllBytes(_inputFile); 
using MemoryStream ms = new MemoryStream(data); 
using CodecsImageInfo _info = _codecs.GetInformation(ms, false); 
 
// Get the proper RasterImageFormat and the friendly name for it 
RasterImageFormat format = _info.Format; 
string extension = RasterCodecs.GetExtension(format); 
Console.WriteLine($"This file has the extension {extension}"); 

Run the Project

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

If the steps were followed correctly, the application runs and gathers the image file format and extension and displays it to the console.

Wrap-Up

This tutorial showed how to gather the image file format and extension using the CodecsImageInfo class and GetExtension() method. We also 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.