Detect Image Format and Extension - Console C#

This tutorial shows how to read a given image file and detect the proper format, along with the appropriate file extension, in a C# Windows Console 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 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS

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 the Detect Image Format and Extension - Console C# 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>\LEADTOOLS21\Bin\Dotnet4\x64:

You will need the individual file codecs for each file type that you are expecting to work with. The toolkit loads the codecs in a certain order when detecting a file format. The following are loaded first:

Note

The individual file codecs do not have to be added to the project directly. They can simply reside in the build folder and will be called at runtime. For more information about loading particular file codecs, including preloading and ignoring codecs, refer to Loading File Codecs.

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:

Note

Adding LEADTOOLS NuGet and local references and setting a license are covered in more detail in the Add References and Set a License tutorial.

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:\LEADTOOLS21\Resources\Images\ocr1.tif

C#
static void Main(string[] args) 
{ 
   try 
   { 
      string _inputFile = @"C:\LEADTOOLS21\Resources\Images\ocr1.tif"; 
 
      SetLicense(); 
 
      // 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}"); 
      } 
   }  
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.Message); 
      Console.WriteLine(ex.StackTrace); 
      Console.ReadLine(); 
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

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 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.