Click here to Skip to main content
15,881,852 members
Articles / Multimedia / GDI+
Article

A simple TIFF management class

Rate me:
Please Sign up or sign in to vote.
4.73/5 (39 votes)
15 Dec 2004 250.6K   7.4K   82   42
Using GDI+ to parse TIFF files.

Introduction

Compared to legacy GDI, the GDI+ that comes with .NET framework is a lot more powerful. This article describes a very simple class that will be used to parse the multi-page TIFF files. The operations include split, join and convert TIFF compression encoder.

Using the code

For those of you playing with fax or OCR software, you might be bored with the various TIFF encoder formats. EncoderValue provided by GDI+ contains most of the compression encoder information that applies to an image. By default, the images saved with Image.Save() are using the LZW encoder while most of the fax software in the market use CCITT standard. Therefore, you should consider carefully when you are dealing with a TIFF image. Nearly all the methods in the TiffManager provide an encoder parameter to handle the encoding part.

C#
/// <summary>
/// This function will output the image
/// to a TIFF file with specific compression format
/// </summary>
/// <param name="outPutDirectory">
///     The splited images' directory</param>
/// <param name="format">The codec for compressing</param>
/// <returns>splited file name array list</returns>
public ArrayList SplitTiffImage(string outPutDirectory,EncoderValue format)
{
  string fileStartString = outPutDirectory + "\\" + 
                           GetFileNameStartString(_ImageFileName);
  ArrayList splitedFileNames=new ArrayList();
  try
  {
    Guid objGuid=image.FrameDimensionsList[0];
    FrameDimension objDimension=new FrameDimension(objGuid);

    //Saves every frame as a separate file.
    Encoder enc=Encoder.Compression;
    int curFrame=0;
    for (int i=0;i<_PageNumber;i++)
    {
      image.SelectActiveFrame(objDimension,curFrame);
      EncoderParameters ep=new EncoderParameters(1);
      ep.Param[0]=new EncoderParameter(enc,(long)format);
      ImageCodecInfo info=GetEncoderInfo("image/tiff");

      //Save the master bitmap
      string fileName=string.Format("{0}{1}.TIF", 
             fileStartString, i.ToString());
      image.Save(fileName,info,ep);
      splitedFileNames.Add(fileName);
      curFrame++;
    }
  }
  catch (Exception)
  {
    throw;
  }

  return splitedFileNames;
}

The various signatures of the TiffManager are listed below:

C#
public void ConvertTiffFormat(string strNewImageFileName, 
                              EncoderValue compressEncoder);
public Image GetSpecificPage(int pageNumber);
public void JoinTiffImages(string[] imageFiles, string outFile, 
                           EncoderValue compressEncoder);
public void RemoveAPage(int pageNumber, EncoderValue compressEncoder, 
                        string strFileName);
public ArrayList SplitTiffImage(string outPutDirectory, EncoderValue format);

Points of Interest

This is really a simple TIFF handling class. You can dive deeper by implementing resolution conversions etc.

History

  • Dec.15, 2004.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Trying to be a .NET developer

Comments and Discussions

 
GeneralI want to write it response.write Pin
rajendra Kulkarni21-Jun-10 15:44
rajendra Kulkarni21-Jun-10 15:44 
GeneralNonsense, it can do nothing Pin
Frenet27-Mar-09 17:02
Frenet27-Mar-09 17:02 
QuestionHow to use the code? Pin
_rush_29-Dec-08 22:36
_rush_29-Dec-08 22:36 
GeneralProblem reading TIFF Imagees Pin
malkowitz7-Dec-08 21:23
malkowitz7-Dec-08 21:23 
GeneralRe: Problem reading TIFF Imagees [modified] Pin
Jason Vogel9-Dec-08 8:15
Jason Vogel9-Dec-08 8:15 
QuestionRe: Problem reading TIFF Imagees Pin
ahmarshi5-Feb-09 4:44
ahmarshi5-Feb-09 4:44 
AnswerRe: Problem reading TIFF Imagees [modified] Pin
ckcool26-May-09 20:52
ckcool26-May-09 20:52 
QuestionCan C# read geoTiff file? Pin
Frank Meng14-Sep-07 19:22
Frank Meng14-Sep-07 19:22 
GeneralRe: Can C# read geoTiff file? Pin
Will Wayne25-Mar-08 11:54
Will Wayne25-Mar-08 11:54 
Questioni cant't merge MultiFrame tif? Pin
jackiehali22-Nov-06 19:13
jackiehali22-Nov-06 19:13 
AnswerRe: i cant't merge MultiFrame tif? Pin
Excise14-Nov-07 8:17
Excise14-Nov-07 8:17 
AnswerRe: i cant't merge MultiFrame tif? Pin
Will Wayne25-Mar-08 12:14
Will Wayne25-Mar-08 12:14 
GeneralGetFileNameStartString have a bug. Pin
Freddy Hernandez22-Nov-06 4:12
Freddy Hernandez22-Nov-06 4:12 
QuestionFind out compression type of TIFF file? Pin
GiGgZ17-Feb-06 6:15
GiGgZ17-Feb-06 6:15 
AnswerRe: Find out compression type of TIFF file? Pin
paragp14-Sep-06 4:42
paragp14-Sep-06 4:42 
GeneralRe: Find out compression type of TIFF file? Pin
Michael Epner18-Sep-06 6:57
Michael Epner18-Sep-06 6:57 
GeneralRe: Find out compression type of TIFF file? Pin
vgebrev13-Jul-07 3:09
vgebrev13-Jul-07 3:09 
GeneralRe: Find out compression type of TIFF file? Pin
H@is@here28-Sep-07 1:34
H@is@here28-Sep-07 1:34 
GeneralRe: Find out compression type of TIFF file? Pin
Will Wayne25-Mar-08 12:19
Will Wayne25-Mar-08 12:19 
GeneralRe: Find out compression type of TIFF file? Pin
Alan Burkhart3-Aug-09 14:46
Alan Burkhart3-Aug-09 14:46 
GeneralRe: Find out compression type of TIFF file? Pin
H@is@here4-Aug-09 2:25
H@is@here4-Aug-09 2:25 
AnswerRe: Find out compression type of TIFF file? Pin
Goran Bacvarovski20-May-08 8:59
Goran Bacvarovski20-May-08 8:59 
GeneralDispose() error when image left unassigned Pin
Arjo Kalisvaart19-Aug-05 0:05
professionalArjo Kalisvaart19-Aug-05 0:05 
General16bpp tiff greyscale Pin
Niles Davis1-Aug-05 0:59
Niles Davis1-Aug-05 0:59 
GeneralGeneral GDI error Pin
Philip W15-Jul-05 4:18
Philip W15-Jul-05 4:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.