ASP.NET C# Helper Class to merge TIFF files into a single multipage TIFF A small yet useful Helper Class written in C-Sharp that can be used to merge multiple TIFF image files into a single multipage TIFF file

Classe ASP.NET C# per il controllo e il calcolo formale del Codice Fiscale

Multipage TIFFs are awesome, aren't them? Well, actually not so much, especially considering the existance of better alternatives such as PDF files. That's precisely why we published a post some months ago explaining how to merge image files of any kind into a single PDF file in ASP.NET C# using the iTextSharp open-source library: if you need a single generic container to store your TIFFs and you can deal with PDFs, we strongly suggest to look no further than that post.

However, there are some specific scenarios where you cannot use PDFs and/or you need to stick to TIFFs. That happened to me some days ago, when I had to re-factor a web service who was serving TIFF files to an old program that could only handle TIFF files.

Since I couldn't use the above PDF-container class, I resorted to code the following helper class, which I'm publishing here hoping that it will help other developers who need to do the same.

That's about it: the comments are quite self-explanatory, hence there's not much to add here.

In case you find this class useful, don't forget to give us a like!

UPDATE: this helper class has been integrated into MergeTIFF, a lightweight .NET Core console application to merge multiple TIFF files into a single multi-page TIF file! For more info, read this post and/or take a look at the MergeTIFF GitHub repository.

 

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

7 Comments on “ASP.NET C# Helper Class to merge TIFF files into a single multipage TIFF A small yet useful Helper Class written in C-Sharp that can be used to merge multiple TIFF image files into a single multipage TIFF file

  1. Thanks for the class, I’m trying to merge tiff images and it is only saving the last image passed in. Here is the code I’m trying.

    string image1 = @”C:\scanned\SCN0001A.tif”;
    string image2 = @”C:\scanned\SCN0002A.tif”;
    string image3 = @”C:\scanned\SCN0002B.tif”;
    string image4 = @”C:\scanned\SCN0003A.tif”;
    byte[][] imagesbytearray = new byte[][] { File.ReadAllBytes(image1), File.ReadAllBytes(image2), File.ReadAllBytes(image3), File.ReadAllBytes(image4) };
    byte[] MergedFile = TiffHelper.MergeTiff(imagesbytearray);
    System.Drawing.Image tiffImage = System.Drawing.Image.FromStream(new MemoryStream(MergedFile));
    tiffImage.Save(@”c:\scanned\test.tif”, System.Drawing.Imaging.ImageFormat.Tiff);

    1. Hello there, the code has been updated to fix this specific issue. Try the new code, it should work fine now!

  2. This code definitely does not do what it says. In order to get a multipage tiff to output the first call to the first call to tiffImage.Save needs to include the the MultiFrame EncoderParameter like this: new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);

    1. Hello there,

      thanks for your feedback: we updated the code, now it should definitely work as expected. We’ll release a GitHub mini-project tomorrow with a sample implementation to further clarify how to use it.

      UPDATE: here’s the GitHub project! Take a look.

  3. Hi Ryan,
    Thank you for the reference code. The Github project has helped us a lot to merge multiple TIFF files into single TIFF file.
    Great work done!
    Thank you.

  4. Hi I have used the same code but it increases the size of the Tiff images when compared to original image.
    I have converted a array of base64 jpg to array of tiff images and i have merged the array of tiff images to single multipage tiff. but actual size of all the 3 images i used is 1.1mb but the final tiff image size is 6.6 mb. what would be the cause and solution to fix it

    1. It might have something to do with the TIFF compression algorithm used by the source images (ZIP, LZW or NONE) compared to the one used in my sample code.

      I suggest to use a 2D image handling software like IrfanView to determine the kind of compression of the original TIFF files to see how they were encoded in the first place:
      https://www.irfanview.com/

      Once done, take a look at the System.Drawing.Imaging.Encoder.Compression official documentation to learn how to modify my sample code in order to setup a suitable compression:
      https://docs.microsoft.com/en-us/dotnet/api/system.drawing.imaging.encoder.compression

      Hope these suggestions can help you!

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.