5,427,303 members and growing! (18,246 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

A simple TIFF management class

By NotProfessional

Using GDI+ to parse TIFF files.
C#, .NET, WinXP, Windows, Visual Studio, GDI+, Dev

Posted: 15 Dec 2004
Updated: 15 Dec 2004
Views: 65,018
Bookmarked: 36 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
24 votes for this Article.
Popularity: 5.79 Rating: 4.19 out of 5
1 vote, 4.2%
1
2 votes, 8.3%
2
1 vote, 4.2%
3
8 votes, 33.3%
4
12 votes, 50.0%
5

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.

/// <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:

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

About the Author

NotProfessional


Trying to be a .NET developer
Occupation: Web Developer
Location: United States United States

Other popular GDI+ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionCan C# read geoTiff file?memberFrank Meng20:22 14 Sep '07  
GeneralRe: Can C# read geoTiff file?memberWill Wayne12:54 25 Mar '08  
Generali cant't merge MultiFrame tif?memberjackiehali20:13 22 Nov '06  
GeneralRe: i cant't merge MultiFrame tif?memberExcise9:17 14 Nov '07  
GeneralRe: i cant't merge MultiFrame tif?memberWill Wayne13:14 25 Mar '08  
GeneralGetFileNameStartString have a bug.memberFreddy Hernandez5:12 22 Nov '06  
GeneralFind out compression type of TIFF file?memberGiGgZ7:15 17 Feb '06  
GeneralRe: Find out compression type of TIFF file?memberparagp5:42 14 Sep '06  
GeneralRe: Find out compression type of TIFF file?memberMichael Epner7:57 18 Sep '06  
GeneralRe: Find out compression type of TIFF file?memberVelislavG4:09 13 Jul '07  
GeneralRe: Find out compression type of TIFF file?memberH@is@here2:34 28 Sep '07  
GeneralRe: Find out compression type of TIFF file?memberWill Wayne13:19 25 Mar '08  
GeneralRe: Find out compression type of TIFF file?membermaxxnostra9:59 20 May '08  
GeneralDispose() error when image left unassignedsussArjo Kalisvaart1:05 19 Aug '05  
General16bpp tiff greyscalememberNiles Davis1:59 1 Aug '05  
GeneralGeneral GDI errormemberPhilip W5:18 15 Jul '05  
GeneralRe: General GDI errormemberPhilip W23:15 30 Aug '05  
GeneralRe: General GDI errormemberNotProfessional17:08 15 Sep '05  
GeneralRe: General GDI errormemberStephanGreiwe3:07 17 May '06  
Generalgood information & nice articlememberjdalberg3:17 22 Feb '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Dec 2004
Editor: Smitha Vijayan
Copyright 2004 by NotProfessional
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project