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 |
|
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 6 tutorial.
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:
Leadtools.Formats.Raster.Common
For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.
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:
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
.
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.
static void Main(string[] args)
{
InitLEAD();
string[] files = Directory.GetFiles(@"C:\LEADTOOLS22\Resources\Images\", "*.cmp");
string multipageFile = @"C:\LEADTOOLS22\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.
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 implementsIDisposable
, make sure it is in ausing
statement for proper disposal.
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:\LEADTOOLS22\Resources\Images
directory and appends each image to a single multipage TIFF file.
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.