Create a Multipage File from Multiple Images - Console C#

This tutorial shows how to create a C# Windows Console application that uses the RasterCodecs class to merge images into one multipage file.

Overview  
Summary This tutorial covers how to merge images into a multipage file 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 the Add References and Set a License tutorial, before working on the Create a Multipage File from Multiple Images - Console C# 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 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>\LEADTOOLS22\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 Merge 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 MergeFiles(string[] files, string outputFile).

Create an array of file names to merge. For this tutorial, all CMP files in "<INSTALL_DIR>\LEADTOOLS22\Resources\Images" will be merged.

C#
// Using block at the top 
using System; 
using System.IO; 
using Leadtools; 
using Leadtools.Codecs; 
C#
static void Main(string[] args) 
{ 
   SetLicense(); 
 
   string[] files = Directory.GetFiles(@"C:\LEADTOOLS22\Resources\Images\", "*.cmp"); 
   string multipageFile = @"C:\LEADTOOLS22\Resources\Images\merged.tif"; 
   MergeFiles(files, multipageFile); 
} 
C#
static void MergeFiles(string[] files, string outputFile) 
{ 
   using (RasterCodecs codecs = new RasterCodecs()) 
      foreach (var file in files) 
         using (RasterImage image = codecs.Load(file)) 
            codecs.Save(image, outputFile, RasterImageFormat.TifJpeg411, 0, 1, -1, 1, CodecsSavePageMode.Append); 
} 

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

Handling Streams

If your application is using a filestream you can use a memory stream to read and write files.

C#
            // Convert a list of files to a list of streams 
            var streams = new List<Stream>(); 
            foreach(var file in files) 
               streams.Add(new MemoryStream(File.ReadAllBytes(file))); 
            // Empty stream to be filled by the codecs 
            var outStream = new MemoryStream(); 
            // Write the stream to file 
            File.WriteAllBytes(multipageFile, outStream.ToArray()); 

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 either creates a new file named merged.tif, or appends pages to that file if it already exists. This file should contain all the images of the CMP files from the LEADTOOLS Images directory.

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.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.