How to Split Multi-page Tiff Image to multiple Images in C#.

In this example we will see that how we can break Multi Page Tiff Image to separate series of images.

Steps of conversion:

1) Create a new console application in Visual Studio 2010.

1

2) Define your input tiff file location and output images location. It should be something like this

static void Main(string[] args)
      {

          string inputTiffFileName = @"C:\Users\Ahmed\Desktop\MultipageTIFImage.tif";
          string outputLocation = @"C:\Users\Ahmed\Desktop\";
          

      }

3) Now make a method which breaks tiff separate images and call it from Main.

static void TiffToImage(string inputTiffFileName, string outputLocation)
       {
           var imageFile = Image.FromFile(inputTiffFileName);
           var frameDimensions = new FrameDimension(imageFile.FrameDimensionsList[0]);
           var numberOfFrames = imageFile.GetFrameCount(frameDimensions);
           var paths = new string[numberOfFrames];
            for (int intFrame = 0; intFrame < numberOfFrames; ++intFrame)
           {
               imageFile.SelectActiveFrame(frameDimensions, intFrame);
               var bmp = new Bitmap(imageFile);
               paths[intFrame] =   outputLocation + intFrame + ".jpg";
               bmp.Save(paths[intFrame], ImageFormat.Jpeg);
               bmp.Dispose();
           }
            imageFile.Dispose();
       }

   }

In this method we have simply calculated the no. of frames in tiff image and converted the active frame to Image

So now our complete code will be Something like this

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

namespace TifftoImage
{
    class Program {
        static void Main(string[] args)
        {

            string inputTiffFileName = @"C:\Users\Ahmed\Desktop\MultipageTIFImage.tif";
            string outputLocation = @"C:\Users\Ahmed\Desktop\";
            TiffToImage(inputTiffFileName, outputLocation);

        }

        static void TiffToImage(string inputTiffFileName, string outputLocation)
        {
            var imageFile = Image.FromFile(inputTiffFileName);
            var frameDimensions = new FrameDimension(imageFile.FrameDimensionsList[0]);
            var numberOfFrames = imageFile.GetFrameCount(frameDimensions);
            var paths = new string[numberOfFrames];
             for (int intFrame = 0; intFrame < numberOfFrames; ++intFrame)
            {
                imageFile.SelectActiveFrame(frameDimensions, intFrame);
                var bmp = new Bitmap(imageFile);
                paths[intFrame] =   outputLocation + intFrame + ".jpg";
                bmp.Save(paths[intFrame], ImageFormat.Jpeg);
                bmp.Dispose();
            }
             imageFile.Dispose();
        }
    }
}

Just Debug the application and we will get our desired output.

I have attached the sample application and multipage tiff image.

Sample+TiffImage.rar (461.69 kb)

Posted by: Mohd Ahmed

Categories: C#, Console Application

Tags: , ,