Create a Multipage File from Multiple Images - C# .NET 6

This tutorial shows how to merge images into a single multipage file in a C# .NET 6 application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to use the RasterCodecs class to merge images into a multipage file 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 this 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 Merge 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 Leadtools; 
using Leadtools.Codecs; 

Inside the Main() method create a string array that will contain the file paths to all the CMP files in the given directory. Then, create a new string value that will contain the file path to output the single multipage TIFF file.

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
 
   string[] files = Directory.GetFiles(@"C:\LEADTOOLS23\Resources\Images\", "*.cmp"); 
   string multipageFile = @"C:\LEADTOOLS23\Resources\Images\merged.tif"; 
   MergeFiles(files, multipageFile); 
} 

Add a new method in the Program class, named MergeFiles(string[] files, string outputFile). This method will be called inside the Main() method as shown above. Add the code below to the new method to grab each CMP file from the given directory and append each image to a multipage TIFF file.

C#
static void MergeFiles(string[] files, string outputFile) 
{ 
   using RasterCodecs codecs = new RasterCodecs(); 
   foreach (var file in files) 
   { 
         Console.WriteLine($"Adding file: {file}"); 
         using RasterImage image = codecs.Load(file); 
         codecs.Save(image, outputFile, RasterImageFormat.TifJpeg411, 0, 1, -1, 1, CodecsSavePageMode.Append); 
   } 
} 

Note

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 loads each CMP file from the C:\LEADTOOLS23\Resources\Images directory and appends each image to a single multipage TIFF file.

Wrap-up

This tutorial showed how to add the necessary references to save TIFF images as well as how to merge images into a multipage file using the RasterImage and RasterCodecs classes.

See Also

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