How to merge TIFF images using C#

TIFF Merger .NET API

GroupDocs.Merger allows developers to combine multiple TIFF documents in the preferred order and save them as a single file.

About TIFF File Format

TIFF, Tag Image File Format, is a computer file used to store raster graphics and image information. A favorite among photographers, TIFFs are a handy way to store high-quality images before editing if you want to avoid lossy file formats.

Download and Configure

Use the downloads section to download API DLLs or MSI installer or NuGet:

PM> Install-Package GroupDocs.Merger

Source TIFF images

“Sample1.tiff” “Sample2.tiff” “Sample3.tiff”

How to merge TIFF files in vertical mode

The following example demonstrates how to merge image files in vertical mode with several lines of C# code:

  • Create an instance of Merger class and pass source image file path as a constructor parameter. You may specify absolute or relative file path as per your requirements.
  • Create an instance of ImageJoinOptions class and pass enum value of ImageJoinMode as a constructor parameter.
  • Add another image file to merge with Join method and pass instance of ImageJoinOptions class as a method parameter. Repeat this step for other image documents you want to merge.
  • Call Merger class Save method and specify the filename for the merged image file as parameter.
// Load the source image file
using (Merger merger = new Merger(@"c:\sample1.tiff"))
{
    // Define image join options with vertical join mode
    ImageJoinOptions joinOptions = new ImageJoinOptions(ImageJoinMode.Vertical);
    // Add another image file to merge
    merger.Join(@"c:\sample2.tiff", joinOptions);
    // Add next image file to merge
    merger.Join(@"c:\sample3.tiff", joinOptions);
    // Merge image files and save result
    merger.Save(@"c:\merged.tiff");
}

Result merged TIFF image

“Merged.tiff”

How to merge TIFF files page by page

A TIFF file can contain several pages (frames). When you join TIFF files without ImageJoinOptions, their pages are appended one after another into a multi-page TIFF:

// Load the source TIFF file
using (Merger merger = new Merger(@"c:\sample1.tiff"))
{
    // Append all pages of another TIFF file
    merger.Join(@"c:\sample2.tiff");
    // Save the multi-page result
    merger.Save(@"c:\merged.tiff");
}

To choose which pages to keep and in which order, use PageBuilder. Pages can be taken from any of the files, including the first one. The document is identified by its index (starting from 0) and the page by its number (starting from 1):

using (Merger merger = new Merger(@"c:\sample1.tiff"))
{
    merger.Join(@"c:\sample2.tiff");

    PageBuilder pageBuilder = merger.CreatePageBuilder();
    // Page 3 of the first file, page 2 of the second file, then page 1 of the first file
    pageBuilder.AddPage(pageBuilder.Documents[0].Index, 3);
    pageBuilder.AddPage(pageBuilder.Documents[1].Index, 2);
    pageBuilder.AddPage(pageBuilder.Documents[0].Index, 1);

    merger.ApplyPageBuilder(pageBuilder);
    merger.Save(@"c:\merged.tiff");
}
Warning
PageBuilder is not available after a join with ImageJoinOptions: the images are then composed into one layout rather than a page sequence, so CreatePageBuilder and ApplyPageBuilder throw GroupDocsMergerException. Join TIFF files without ImageJoinOptions to compose them page by page.

Code Examples

Please find more use-cases and complete C# sources of our backend and frontend examples and try them for free!

Merge TIFF Live Demo

GroupDocs.Merger for .NET provides an online TIFF Merger App, which allows you to try it for free and check its quality and accuracy.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.