Split a Multipage Image File Into Separate Files - C# .NET 6

This tutorial shows how to save each page of a multipage image into separate image files in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to split multipage image files using the RasterCodecs class in a C# .NET 6 Console application.
Completion Time 30 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 the Split a Multipage Image File Into Separate Files - C# .NET 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 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 Split Image 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; 

In the Program class, add a new string fileName, below the set license code. Set the new string value to the file path containing the multipage image file. Add a new method named SplitFile(string inputFile) to the Program class. Call it in the Main() method after the fileName string value, as shown below.

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   string multipageFile = @"FILE PATH TO MULTIPAGE IMAGE FILE"; 
   SplitFile(multipageFile); 
} 

Add the code below to the SplitFile() method to load each page individually from a multipage image file and export the page as its own image file.

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:\LEADTOOLS23\Resources\Images\{Path.GetFileNameWithoutExtension(inputFile)}_page{page}.png"; 
         using (RasterImage image = codecs.Load(inputFile, page)) 
            codecs.Save(image, outputFile, RasterImageFormat.Png, 0); 
      } 
   } 
} 

Note

To test the code above, 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.

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 console appears and the application creates new PNG image files from each page of the multipage image file.

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 using the RasterImage and RasterCodecs classes.

See Also

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