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

This tutorial shows how to create a C# Windows Console application that uses theRasterCodecsclass 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 (3 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 theAdd References and Set a Licensetutorial, before working on theSplit a Multipage Image File Into Separate Files - Console C#教程。

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in theAdd References and Set a License教程。如果这个项目is unavailable, 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). 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\LEADTOOLS22\Bin\Dotnet4\x64:

For a complete list of which Codec DLLs are required for specific formats, refer toFile 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 toSetting a Runtime License.

有两种类型的运行时许可证:

Add the Split Image Code

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

OpenProgram.csin theSolution Explorerand then addusing Leadtools;andusing Leadtools.Codecs;statements to the using block at the top.

In the Program class add a new method calledSplitFile(string inputFile)and call it in the Main method afterSetLicense().

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 theCreate a Multipage File from Multiple Images教程。


C#
// Using block at the topusingSystem;usingSystem.IO;usingLeadtools;usingLeadtools.Codecs;
C#
staticvoidMain(string[] args){SetLicense();stringmultipageFile =@"C:\LEADTOOLS22\Resources\Images\merged.tif";SplitFile(multipageFile);}
C#
staticvoidSplitFile(stringinputFile){using(RasterCodecs codecs =newRasterCodecs()){inttotalPages = codecs.GetTotalPages(inputFile);for(intpage = 1; page <= totalPages; page++){stringoutputFile = $@"C:\LEADTOOLS22\Resources\Images\{Path.GetFileNameWithoutExtension(inputFile)}_page{page}.png";using(RasterImage image = codecs.Load(inputFile, page))codecs.Save(image, outputFile, RasterImageFormat.Png, 0);}}}

Because theRasterCodecsclass implementsIDisposable, make sure it is in ausingstatement for proper disposal.

Handling Streams

To handle the files usingMemoryStream,添加一个新的SplitFileStreammethod and replace the existing code in theMain()method with the following:

C#
staticvoidMain(string[] args){try{SetLicense();stringmultipageFile =@"C:\LEADTOOLS22\Resources\Images\MultipageImage.tif";byte[] multipageData = File.ReadAllBytes(multipageFile);using(MemoryStream multipageStream =newMemoryStream(multipageData))SplitFileStream(multipageStream);}catch(Exception ex){Console.WriteLine(ex.ToString());}Console.WriteLine("Press any key to exit...");Console.ReadKey(true);}staticvoidSplitFileStream(Stream inputStream){using(RasterCodecs codecs =newRasterCodecs()){inttotalPages = codecs.GetTotalPages(inputStream);for(intpage = 1; page <= totalPages; page++){using(MemoryStream outputStream =newMemoryStream())using(RasterImage image = codecs.Load(inputStream, page)){codecs.Save(image, outputStream, RasterImageFormat.Png, 0);// outputStream can be used here before it is automatically freed to hold next page in a new stream}}}}

Run the Project

Run the project by pressingF5, or by selectingDebug->Start Debugging.

If the steps were followed correctly, the application runs and creates new files. Each page ofmerged.tifshould 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

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2023LEAD Technologies, Inc.All Rights Reserved.