Use LibTIff in C# to convert from one tiff format to another
- by Kevin
I have a Tiff using JPEG format the WPF / C# can not handle via TiffBitmapDecoder. Our clients use the file format and our current C++ and Java code handles it. I need to convert this to a format I can display using TiffBitmapDecoder or standard BitmapImage. It looks like the C# version of LibTiff is the way to go but I am not having any luck converting in code.
Here is my attempt - I always end up with corrupt files.
`
Boolean doSystemLoad = false;
Tiff tiff = null;
try
{
tiff = Tiff.Open(file, "r");
}
catch (Exception e) // TIFF could not handle, let OS do it
{
doSystemLoad = true;
}
if (tiff != null)
{
width = Double.Parse(tiff.GetField(TiffTag.IMAGEWIDTH)[0].Value.ToString());
height = Double.Parse(tiff.GetField(TiffTag.IMAGELENGTH)[0].Value.ToString());
int bits = Int32.Parse(tiff.GetField(TiffTag.BITSPERSAMPLE)[0].Value.ToString());
int samples = Int32.Parse(tiff.GetField(TiffTag.SAMPLESPERPIXEL)[0].Value.ToString());
string compression = tiff.GetField(TiffTag.COMPRESSION)[0].Value.ToString();
Console.WriteLine("Image is " + width + " x " + height + " bits " + bits + " sample " + samples);
Console.WriteLine("Compression " + compression);
// We allow OS to load anything that is not JPEG compression
doSystemLoad = compression.ToLower().IndexOf("jpeg") == -1;
string tempFile = Path.GetTempFileName() + ".tiff";
// Convert here then load converted via OS
if (!doSystemLoad)
{
Console.WriteLine(">> Attempting to convert... " + tempFile);
Console.WriteLine(" Scan line " + tiff.ScanlineSize());
Tiff tiffOut = Tiff.Open(tempFile, "w");
tiffOut.SetField(TiffTag.IMAGEWIDTH, width);
tiffOut.SetField(TiffTag.IMAGELENGTH, height);
tiffOut.SetField(TiffTag.BITSPERSAMPLE, bits);
tiffOut.SetField(TiffTag.SAMPLESPERPIXEL, samples);
tiffOut.SetField(TiffTag.ROWSPERSTRIP, 1L);
tiffOut.SetField(TiffTag.COMPRESSION, Compression.NONE);
tiffOut.SetField(TiffTag.ORIENTATION, BitMiracle.LibTiff.Classic.Orientation.TOPLEFT);
tiffOut.SetField(TiffTag.FAXMODE, FaxMode.CLASSF);
tiffOut.SetField(TiffTag.GROUP3OPTIONS, 5);
tiffOut.SetField(TiffTag.PHOTOMETRIC, Photometric.RGB);
tiffOut.SetField(TiffTag.FILLORDER, FillOrder.MSB2LSB);
tiffOut.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);
tiffOut.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);
tiffOut.SetField(TiffTag.XRESOLUTION, 100.0);
tiffOut.SetField(TiffTag.YRESOLUTION, 100.0);
tiffOut.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
tiffOut.SetField(TiffTag.PAGENUMBER, new object[] { 1, 1 });
tiffOut.SetField(TiffTag.PAGENAME, "Page 1");
Byte[] scanLine = new Byte[tiff.ScanlineSize() + 5000];
for (int row = 0; row < height; row++)
{
tiff.ReadScanline(scanLine, row);
tiffOut.WriteScanline(scanLine, row);
}
tiffOut.Dispose();
}
tiff.Dispose();
Stream imageStreamSource = new FileStream(tempFile, FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
width = bitmapSource.Width;
height = bitmapSource.Height;
imageMain.Width = width;
imageMain.Height = height;
imageMain.Source = bitmapSource;
}
if (doSystemLoad)
{
Stream imageStreamSource = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
BitmapSource bitmapSource = decoder.Frames[0];
width = bitmapSource.Width;
height = bitmapSource.Height;
imageMain.Width = width;
imageMain.Height = height;
imageMain.Source = bitmapSource;
}
`