Split a Multipage Image File Into Separate Files - Console C#

This tutorial shows how to create a C# Windows Console application that uses the RasterCodecs class to save each page of a multipage image into a separate image file.

Overview  
Summary This tutorial covers how to split multipage image files in a C# Windows Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (2 KB)
Platform C# Windows Console 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 Split a Multipage Image File Into Separate Files - Console C# tutorial.

Create the Project and Add LEADTOOLS References

Create a new C# Windows Console project, and add the below necessary LEADTOOLS references.

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). For this project, the following references are needed:

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

If local DLL references are used, the following DLLs are needed. The DLLs are located at <INSTALL_DIR>\LEADTOOLS21\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer to File Format Support.

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 Split Image Code

With the project created, the references added, and the license set, coding can begin.

Open Program.cs in the Solution Explorer and then add using Leadtools; and using Leadtools.Codecs; statements to the using block at the top.

In the Program class add a new method called SplitFile(string inputFile) and call it in the Main method after SetLicense().

Note

To try the code below, use a multipage file, such as TIFF or PDF. If such file is not available, create one by following the Create a Multipage File from Multiple Images Tutorial.


C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   string multipageFile = @"C:\LEADTOOLS21\Resources\Images\merged.tif"; 
   SplitFile(multipageFile); 
} 
C#
static void SplitFile(string inputFile) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
   { 
      int totalPages = codecs.GetTotalPages(inputFile); 
      for (int page = 1; page <= totalPages; page++) 
      { 
         string outputFile = $@"C:\LEADTOOLS21\Resources\Images\{Path.GetFileNameWithoutExtension(inputFile)}_page{page}.png"; 
         using (RasterImage image = codecs.Load(inputFile, page)) 
            codecs.Save(image, outputFile, RasterImageFormat.Png, 0); 
      } 
   } 
} 

Because the RasterCodecs class implements IDisposable, make sure it is in a using statement for proper disposal.

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 creates new files. Each page of merged.tif should be created as a PNG image file with the page number appended to the name.

Wrap-up

This tutorial showed how to add the necessary references to load all the pages of a TIFF image file and split them into separate PNG images.

See Also

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