Author: Kerry D. Wong

Downloads:
Complete Source: tiffms_src.zip
Complete Binaries: tiffms_setup.exe
Command-line Only: tiffms_cmd.exe

This application is released under BSD License.

Introduction

As the name indicates, this utility provides functionality to

1. Extract pages from multi-frame TIFF file(s) (TIFF file that contains multiple pages of images) into single-frame TIFF files.

2. Combine multiple single-frame or multi-frame TIFF files into one multi-frame TIFF file.

This utility comes with a command line executable and a GUI application. The command line version can be used to create automated batch jobs while the GUI version provides user an easy way to manipulate files.

GUI

During setup, an Icon is created under All Programs. The default windows looks like:

TIFFMS 

TIFF files to be converted can be dragged and dropped into the list window. The order of the file list can also be changed by dragging and dropping files to their desired locations (this is useful when combining multiple single or multi-frame TIFFS into one multi-frame TIFF file).

When converting multi-frame TIFF files into single frame TIFF files, multiple input files can be selected, the results will be saved to a folder with the same name as the muti-frame TIFF file with numbers indicating the frame order. For example, if a multi-frame file C:\temp\test.tiff has three frames, the resulting files will be C:\temp\test\test_1.tiff, C:\temp\test\test_2.tiff, C:\temp\test\test_3.tiff.

When converting multiple TIFF files (each file can be either single frame or multi-frame) into a single multi-frame TIFF file, you will need to specify the output file name (including path). In this case, the order at which the files appeared in the list determines the page order in the resulting multi-frame TIFF image.

Command Line:

The default installation folder is C:\Program Files\TIFF Merge Split Utility. And the command application can be launched by TiffMS.exe.

Usage: TiffMS [-h|-s|-o -m] | [-c]
        -h show help
        -s split multi-page tiff file(s) into single-page tiff files
        -o output file name
        -m merge single-page tiff files into a single multi-page tiff file
        -c the configuration file name

Examples:
        TiffMS -s a.tiff
                split a.tiff into a_1.tiff a_2.tiff …
        TiffMS -s a.tiff c:\temp
                split a.tiff and put splitted files into c:\temp.
        TiffMS -o out.tiff -m a.tiff b.tiff
                Merge a.tiff and b.tiff into a single tiff file out.tiff.
        TiffMS -c test.config
                Merge or split files according to settings in test.config

The last option (-c) makes it easy to manipulate files according to a configuration file. The configuration file is a text file and here are two samples:

1. For splitting files:

#Sample

ACTION = SPLIT
SOURCE_DIR = c:\source_dir
DESTINATION_DIR = c:\dest_dir
INPUT = …
          1.tif …
          test1.tif …
          test2.tif

2. For combining multiple files:

#Sample

ACTION = MERGE
SOURCE_DIR = c:\src_dir
INPUT = …
          1.tif …
          test1.tif …
          test2.tif
 
OUTPUT = test.tif

Core Functions Source Code:

        public void ConvertToSinglePageTiffs(string fileName, string destFolder)
        {
            Image image = Image.FromFile(fileName);
            ImageCodecInfo codecInfo = GetCodecInfo(TIFF_CODEC);
 
            FrameDimension frameDim = new FrameDimension(image.FrameDimensionsList[0]);
            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, ENCODING_SCHEME);
 
            for (int i = 0; i < image.GetFrameCount(frameDim); i++)
            {
                image.SelectActiveFrame(frameDim, i);
 
                string fileNameWOExt = Path.GetFileNameWithoutExtension(fileName);
                string newFileName = string.Concat(fileNameWOExt, "_", (i + 1).ToString(), TIFF_FILE_EXTENSION);
 
                string folder = Path.Combine(Path.GetDirectoryName(fileName), destFolder);
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
 
                image.Save(Path.Combine(folder, newFileName), codecInfo, encoderParams);
            }
        }
 
        public void ConvertToMultiPageTiff(List<string> fileNames, string outputFileName)
        {
            ImageCodecInfo codecInfo = GetCodecInfo(TIFF_CODEC);
 
            EncoderParameters encoderParams = new EncoderParameters(2);
            encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long) EncoderValue.MultiFrame);
            encoderParams.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, ENCODING_SCHEME);
 
            Image image = Image.FromFile(fileNames[0]);
 
            if (!Directory.Exists(Path.GetDirectoryName(outputFileName)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(outputFileName));
            }
 
            image.Save(outputFileName, codecInfo, encoderParams);
 
            encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
            FrameDimension frameDim = new FrameDimension(image.FrameDimensionsList[0]);
 
            for (int i = 1; i < image.GetFrameCount(frameDim); i++)
            {
                image.SelectActiveFrame(frameDim, i);
                image.SaveAdd(image, encoderParams);
            }
 
            Image[] images = new Image[fileNames.Count 1];
            for (int i = 1; i < fileNames.Count; i++)
            {
                images[i 1] = Image.FromFile(fileNames[i]);
                frameDim = new FrameDimension(images[i 1].FrameDimensionsList[0]);
 
                for (int j = 0; j < images[i 1].GetFrameCount(frameDim); j++)
                {
                    images[i 1].SelectActiveFrame(frameDim, j);
                    image.SaveAdd(images[i 1], encoderParams);
                }
            }
        }

 

Update:

The default EncoderValue may cause "Parameter is not valid" Exception. Please refer to the latest post for more details.

Be Sociable, Share!