在LibTiff.Net documentation我发现可以获得Tiff文档的特定页面。
但是,如果可以分割多页Tiff(使用LibTiff.Net)而不知道有多少页面?怎么样?
使用this示例,它仅返回第一页。
Btw,主要的问题是Windows XP无法处理不同的tiff图像,所以我想把它分成jpeg图像。答案 0 :(得分:9)
//open tif file
var tif = Tiff.Open(@"file", "r");
//get number of pages
var num = tif.NumberOfDirectories();
for (short i = 0; i < num; i++)
{
//set current page
tif.SetDirectory(i);
Bitmap bmp = GetBitmapFormTiff(tif);
bmp.Save(string.Format(@"newfile{0}.bmp", i));
}
GetBitmapFormTiff的代码来自example:
private static Bitmap GetBitmapFormTiff(Tiff tif)
{
FieldValue[] value = tif.GetField(TiffTag.IMAGEWIDTH);
int width = value[0].ToInt();
value = tif.GetField(TiffTag.IMAGELENGTH);
int height = value[0].ToInt();
//Read the image into the memory buffer
var raster = new int[height * width];
if (!tif.ReadRGBAImage(width, height, raster))
{
return null;
}
var bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmpdata = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
var bits = new byte[bmpdata.Stride * bmpdata.Height];
for (int y = 0; y < bmp.Height; y++)
{
int rasterOffset = y * bmp.Width;
int bitsOffset = (bmp.Height - y - 1) * bmpdata.Stride;
for (int x = 0; x < bmp.Width; x++)
{
int rgba = raster[rasterOffset++];
bits[bitsOffset++] = (byte)((rgba >> 16) & 0xff);
bits[bitsOffset++] = (byte)((rgba >> 8) & 0xff);
bits[bitsOffset++] = (byte)(rgba & 0xff);
bits[bitsOffset++] = (byte)((rgba >> 24) & 0xff);
}
}
System.Runtime.InteropServices.Marshal.Copy(bits, 0, bmpdata.Scan0, bits.Length);
bmp.UnlockBits(bmpdata);
return bmp;
}