Create a TIFF image with multiple images programmatically in C#

Hello Friends,

In this post, I'll show you how to create a TIFF image with multiple pages using C#.

TIFF: Tagged Image File Format [This is used to store images]

So first create new Console Project using Visual Studio.

After you Click on OK button, a console application with Program.cs file will be created as you can see in the following image:

Before converting some image to Tiff you must have some images at any location in your system as I have some image:

Now it's time to write some code, but before writing any code please ensure you successfully added some reference to your solution:

Add System.Drawing in your solution:

After adding the reference use two important reference for creating TIFF file with multiple images in it: 

using System.Drawing;
using System.Drawing.Imaging;

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;

namespace CreateMultiTiffImage
{
class Program
{
static void Main(string[] args)
{
// Default Image for Tiff image
Image bmp = Image.FromFile(@"D:\ConvetToTIff\1.png");
// 2nd and 3rd image that will add to the original Tiff image
Image img1 = Image.FromFile(@"D:\ConvetToTIff\2.bmp");
Image img2 = Image.FromFile(@"D:\ConvetToTIff\3.jpg");

// Assign final TIFF image to BMP
Bitmap bitmap = (Bitmap)bmp;
//Select image encoder
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;

// Select ImageCodecInfo for tiff file format
ImageCodecInfo info = null;
info = (from ie in ImageCodecInfo.GetImageEncoders()
where ie.MimeType == "image/tiff"
select ie).FirstOrDefault();
EncoderParameters encoderparams = new EncoderParameters(1);

//make this file multi frame to be able to add multiple images
encoderparams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);

//Save the bitmap
bitmap.Save(@"D:\ConvetToTIff\MultiFrameImage.tiff", info, encoderparams);
encoderparams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

//add another images and Repeat this process to Add Multiple Images
bitmap.SaveAdd(img1, encoderparams);
bitmap.SaveAdd(img2, encoderparams);

// Close the file
encoderparams.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
bitmap.SaveAdd(encoderparams);

Console.ReadLine();
}
}
}

How your solution look:

 

Now just click on Start button or Hit F5 to run this application:

After click on Start button please open your Folder where all the images are avaiable, in this folder you will new file named: MultiFrameImage.tiff as shown in following figure:

Open this file:

As you can see there are 3 pages in this file.

Download the Solution:

CreateMultiTiffImage.zip (35.27 kb)

Comments (2) -

By23LtrSveZs 7/24/2019 3:12:57 PM

840670 903914Thank you for any other informative blog. Where else  may just I am getting that kind of info written in such a perfect way? 916805

Thank you.
I have used your source very carefully.
I thank you again.

Add comment