我有一个采用JPEG格式的TIFF文件,WPF/C#无法通过TiffBitmapDecoder处理。我们的客户使用该文件格式,我们当前的C++和Java代码可以处理它。
我需要将它转换为一种可以使用TiffBitmapDecoder或标准BitmapImage显示的格式。看起来C#版本的libtiff是正确的选择,但我在转换代码方面遇到了困难。
这是我的尝试 - 我总是得到损坏的文件。
我需要将它转换为一种可以使用TiffBitmapDecoder或标准BitmapImage显示的格式。看起来C#版本的libtiff是正确的选择,但我在转换代码方面遇到了困难。
这是我的尝试 - 我总是得到损坏的文件。
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;
}