Click here to Skip to main content
Click here to Skip to main content

A simple TIFF management class

By , 15 Dec 2004
 

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
Web Developer
United States United States
Member
Trying to be a .NET developer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questioncombine two multipage page tiff in single multipage Tiffmemberarventh14 Mar '12 - 20:14 
AnswerRe: combine two multipage page tiff in single multipage TiffmemberdavH16 Apr '12 - 5:43 
Have a look at this article .
 
I slightly modified the code from above with this one, have fun ! :
 
        // ...

        private void btnLoadImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();
            if (op.ShowDialog() == DialogResult.OK)
            {
                string filename=op.FileName.ToString();
                loadMultipageTiff(filename);
            }
        }
 
        private void loadMultipageTiff(string filename)
        {
            Bitmap bitmap = (Bitmap)Image.FromFile(filename);
            int count = bitmap.GetFrameCount(FrameDimension.Page);
            
            for (int idx = 0; idx < count; idx++)
            {
                // save each frame to a bytestream
                bitmap.SelectActiveFrame(FrameDimension.Page, idx);
                MemoryStream byteStream = new MemoryStream();
                bitmap.Save(byteStream, ImageFormat.Tiff);
 
                // and then create a new Image panel from it
                createPanel(byteStream);
            }
 
        }
 

        int firstpanel = 0;
        private void createPanel(MemoryStream byteStream)
        {
            // Image loadImage = Image.FromFile(filename);
            Image loadImage = Image.FromStream(byteStream);
 
            Panel p = new Panel();
 
            // ...

GeneralMy vote of 5membertyagu_anaykus12 Mar '12 - 5:54 
QuestionDiscovering image disk sizememberMember 859395730 Jan '12 - 11:27 
QuestionWhich EncoderValue to be used for SpllitTiffImagememberRupeshNYadav15 Jul '11 - 1:24 
AnswerRe: Which EncoderValue to be used for SpllitTiffImagememberCheery_an12 Jul '12 - 19:20 
AnswerCool!!memberPalle Hansen26 Apr '11 - 2:21 
GeneralI want to write it response.writememberrajendra Kulkarni21 Jun '10 - 15:44 
GeneralNonsense, it can do nothingmemberFrenet27 Mar '09 - 17:02 
QuestionHow to use the code?member_rush_29 Dec '08 - 22:36 
GeneralProblem reading TIFF Imageesmembermalkowitz7 Dec '08 - 21:23 
GeneralRe: Problem reading TIFF Imagees [modified]memberJason Vogel9 Dec '08 - 8:15 
QuestionRe: Problem reading TIFF Imageesmemberahmarshi5 Feb '09 - 4:44 
AnswerRe: Problem reading TIFF Imagees [modified]memberckcool26 May '09 - 20:52 
QuestionCan C# read geoTiff file?memberFrank Meng14 Sep '07 - 19:22 
GeneralRe: Can C# read geoTiff file?memberWill Wayne25 Mar '08 - 11:54 
Questioni cant't merge MultiFrame tif?memberjackiehali22 Nov '06 - 19:13 
AnswerRe: i cant't merge MultiFrame tif?memberExcise14 Nov '07 - 8:17 
AnswerRe: i cant't merge MultiFrame tif?memberWill Wayne25 Mar '08 - 12:14 
GeneralGetFileNameStartString have a bug.memberFreddy Hernandez22 Nov '06 - 4:12 
QuestionFind out compression type of TIFF file?memberGiGgZ17 Feb '06 - 6:15 
AnswerRe: Find out compression type of TIFF file?memberparagp14 Sep '06 - 4:42 
GeneralRe: Find out compression type of TIFF file?memberMichael Epner18 Sep '06 - 6:57 
GeneralRe: Find out compression type of TIFF file?memberVelislavG13 Jul '07 - 3:09 
GeneralRe: Find out compression type of TIFF file?memberH@is@here28 Sep '07 - 1:34 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 15 Dec 2004
Article Copyright 2004 by NotProfessional
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid