views:

356

answers:

4

I am attempting to open .tif files that have color in them (300 dpi, PixelFormat.Format24bppRgb) using the .Net Image and Bitmap classes. I always get an "invalid parameter" error when the tiffs have color (works fine for black and white tiffs). If anyone has source code on how to open a .tif with color in it I'd deeply appreciate it. Below is what I'm attempting to do; this also fails when calling Bitmap.FromStream:

using (FileStream fs = File.OpenRead(fileName))
{
    using (Image img = Image.FromStream(fs)) {}
}
A: 

The reason why you can't open the image file is that the image maybe has compression format. I also encounter this problem when I open a .gif image file. I'm searching the solutions. We can communicate each other. [email protected] thanks

A: 

I think the basic problem is that one cannot use the .Net Image/Bitmap classes to reliably open color TIFFs. Many of the various compression formats used to encode color TIFFs break the .Net Image/Bitmap class.

It seems to be a catch 22--you have to know the TIFF formatting information to know how to load it, but in .Net one needs to load the Image/Bitmap class to read TIFF formatting information.

I think the answer to my question is, "this can't be done just using the .Net framework," and that I'll need to either buy a 3rd party control or create something myself in C++.

Tom Regan
A: 

I found the GDI(+) the limiting factor in dealing with TIFF. I had many "invalid parameter" issues on XP (in my case because of using 16bpp grayscale and tiled tiff). The same code worked flawlessly on Windows7/64bit.

For me, the free LibTIFF.NET was the solution. (StackOverflow using LibTIFF from c#) Only con compared to GDI+: you'll have to roll your own BMP (uncompressed byte array to bmp conversion). I can provide you with examples if you like.

To diagnose your TIFF file type, you could use TiffDump download link Using like tiffdump myfile.tif will produce an output like:

Magic: 0x4949 <little-endian> Version: 0x2a
Directory 0: offset 1669857288 (0x63880008) next 0 (0)
ImageWidth (256) SHORT (3) 1<33227>
ImageLength (257) SHORT (3) 1<24986>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<1>
FillOrder (266) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
XResolution (282) RATIONAL (5) 1<6400>
YResolution (283) RATIONAL (5) 1<6400>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<2>
TileWidth (322) SHORT (3) 1<256>
TileLength (323) SHORT (3) 1<256>

Most likely such info can get you more specifc answers on StackOverflow.

Adriaan
A: 

Thank you Adriaan, LibTIFF is going to come in handy.

Tom Regan