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 (2 KB) |
Platform | C# Windows Console Application |
IDE | Visual Studio 2017, 2019 |
Development License | Download LEADTOOLS |
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 a new C# Windows Console project, and add the below necessary LEADTOOLS references.
If using NuGet references, this tutorial requires the following NuGet package:
Leadtools.Formats.Raster.Common
If using local DLL references, the following DLLs are needed:
The local DLLs are located at <INSTALL_DIR>\LEADTOOLS 20\Bin\Dotnet4\x64
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.Codecs.Tif.dll
For a complete list of which Codec DLLs are required for specific formats, refer to Files to be Included in 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:
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.
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 "C:\Users\Public\Documents\LEADTOOLS Images"
will be merged.
// Using block at the top
using System;
using System.IO;
using Leadtools;
using Leadtools.Codecs;
static void Main(string[] args)
{
SetLicense();
string[] files = Directory.GetFiles(@"C:\Users\Public\Documents\LEADTOOLS Images", "*.cmp");
string multipageFile = @"C:\Users\Public\Documents\LEADTOOLS Images\merged.tif";
MergeFiles(files, multipageFile);
}
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.
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.
This tutorial showed how to add the necessary references to save TIFF images as well as how to merge images into a multipage file.