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

This tutorial shows how to merge images into a single multipage file in a C# .NET Core 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 Core Console application.
Completion Time 30 minutes
Visual Studio Project Download tutorial project (1 KB)
Platform C# .NET Core 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 Create a Multipage File from Multiple Images - C# .NET Core 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:

Note

Adding LEADTOOLS NuGet references and setting a license are covered in more detail in the Add References and Set a License tutorial.

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 System; 
using System.IO; 
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) 
{ 
   if (!SetLicense()) 
      Console.WriteLine("Error setting license"); 
   else 
      Console.WriteLine("License file set successfully"); 
   try 
   { 
      string[] files = Directory.GetFiles(@"C:\LEADTOOLS21\Resources\Images\", "*.cmp"); 
      string multipageFile = @"C:\LEADTOOLS21\Resources\Images\merged.tif"; 
      MergeFiles(files, multipageFile); 
   } 
   catch (Exception ex) 
   { 
      Console.WriteLine(ex.ToString());   
   } 
   Console.WriteLine("Press any key to exit..."); 
   Console.ReadKey(true); 
} 

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:\LEADTOOLS21\Resources\Images directory and appends each image to a single multi-page 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 21.0.2023.3.1
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.