Detect Image Format and Extension - Console C#

This tutorial shows how to read an image file and detect the proper format and file extension in a C# Windows Console application using the LEADTOOLS SDK.

Note

This feature 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 (3 KB)
Platform Windows Console C# Application
IDE Visual Studio 2017, 2019
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 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>\LEADTOOLS22\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:\LEADTOOLS22\Resources\Images\ocr1.tif

C#
static void Main(string[] args) 
{ 
   try 
   { 
      string _inputFile = @"C:\LEADTOOLS22\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); 
} 

Handling Streams

To handle this using MemoryStream replace the code wrapped in 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 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

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