Panda
Panda

Reputation: 51

Multipage TIFF image file created in .NET Framework has bigger size

I created windows service in c# (.net framework 4.0) for multipage TIFF files creation. Windows servis is monitorig directories and merges or divide single page Tiff files in directories to multipage TIFF files. Service is running on Windows 2012 server.

When I open and then save TIFF file created by mentioned service in another image application (e.g. Irfan). The saved file has smaller size. In Irfan I save it with same resolution and compression as is in original file. When I compare properties of files in explorer they have same dimension, dpi and compression type.

e.g. my multipage Tiff file created in .NET has 426kB and when I open and save the same file in Irfan it has 407kB after that. It is about 20kB difference. For only one file it is not important, but I have 6 hundred thousand files in directory :(

For TIFF creation I'm using System.Windows.Media.Imaging it should be better then System.Drawing.Imaging (GDI+).

...

// load frontside of document
decoder = new TiffBitmapDecoder(imageStreamSource,BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);

bitmapSourceFrontSide = decoder.Frames[currentFrame];

// load backside of document

//...

encoder = new TiffBitmapEncoder();
encoder.Compression = TiffCompressOption.Ccitt4;

CroppedBitmap tiffPage;

//...

// TIFF image can be divided to multipage tiff
tiffPage = new CroppedBitmap(bitmapSourceFrontSide, new Int32Rect(pagePosition, 0, pageFrameWidth, (int)bitmapSourceFrontSide.PixelHeight));

encoder.Frames.Add(BitmapFrame.Create(tiffPage));

if (bitmapSourceBackSide != null)
 {
 tiffPage = new CroppedBitmap(bitmapSourceBackSide, new Int32Rect(pagePosition, 0, pageFrameWidth, (int)bitmapSourceBackSide.PixelHeight));

    encoder.Frames.Add(BitmapFrame.Create(tiffPage));
 }

//... created and add all pages to tiff file let's save it

using(FileStream stream = new FileStream(FileName, FileMode.Create))
{ 
 encoder.Save(stream);
} 

Do you have idea why TIFF file created in .NET has bigger size?

Thank you

Upvotes: 2

Views: 3184

Answers (2)

Panda
Panda

Reputation: 51

It seems that difference is in the internal structure of TIFF file created from .Net Framework.

I compared TIFF tags and file structure created in .Net Framework and other applications (Irfan and LibTff.NET) and .NET Framework internally dived TIFF file to different count of Strips and rows. It is probably internal implementation of Tiff encoder and developer can't change it. It has no effect on image quality but file could be slightly bigger.

The main differences in files are in the following structures:

StripByteCounts

  • Tag = 279 (117)Hex
  • Type = word or dword
  • N = StripsPerImage for PlanarConfiguration equal to 1. = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2
  • No default.

For each strip, the number of bytes in that strip. The existence of this field greatly simplifies the chore of buffering compressed data, if the strip size is reasonable.

StripOffsets

  • Tag = 273 (111)Hex
  • Type = word or dword
  • N = StripsPerImage for PlanarConfiguration equal to 1. = SamplesPerPixel * StripsPerImage for PlanarConfiguration equal to 2
  • No default.

For each strip, the byte offset of that strip. The offset is specified with respect to the beginning of the TIFF file. Note that this implies that each strip has a location independent of the locations of other strips. This feature may be useful for editing applications. This field is the only way for a reader to find the image data, and hence must exist.

RowsPerStrip

  • Tag = 278 (116)Hex
  • Type = word or dword
  • N = 1
  • Default is 2**32 - 1, which is effectively infinity. That is, the entire image is one strip.
  • Recommended is a strip size of 8K.

The number of rows per strip. The image data is organized into strips for fast access to individual rows when the data is compressed - though this field is valid even if the data is not compressed.

Tags description was used from
http://www.fileformat.info/format/tiff/corion.htm

Upvotes: 2

TaW
TaW

Reputation: 54433

I see you are using BitmapCreateOptions.PreservePixelFormat and TiffCompressOption.Ccitt4.
MSDN say about TiffCompressOption Enumeration:

The Ccitt3, Ccitt4, and Rle require that the PixelFormat value be set to BlackWhite. Setting the PixelFormat to any other value resets the Compression property value to Default.

Maybe this is happening? Are the images B/W? If not try using Lzw or Zip explicitly to see if they work better on your images than the default the system decides to use!

Also, imo, a difference of 5% is not really something to get upset about. What it means is that instead of 600,000 images you can only hold 570,000 images.

Upvotes: 2

Related Questions