2

LibTiff.NET用来读取多页 Tiff 文件。将我的 Tiff 转换为 a 没有问题System.Drawing.Bitmap,因为它显示在他们的网站上,但我想要的是 aBitmapSource或与使用 in 相当的东西WPF。当然,我已经可以转换了converted System.Drawing.Bitmap,但是由于数据量很大,我正在寻找一种方法,直接从Tiff object.

有什么建议么?也许使用 ReadRGBAImage 方法,它返回一个带有颜色的 int 数组?

编辑1:

我尝试了以下方法,但只得到了由灰色条纹组成的图像:

int[] raster = new int[height * width];
im.ReadRGBAImage(width, height, raster);
byte[] bytes = new byte[raster.Length * sizeof(int)];

Buffer.BlockCopy(raster, 0, bytes, 0, bytes.Length);

int stride = raster.Length / height;
image.Source = BitmapSource.Create(
     width, height, dpiX/*ex 96*/, dpiY/*ex 96*/,
     PixelFormats.Indexed1, BitmapPalettes.BlackAndWhite, bytes, 
     /*32/*bytes/pixel * width*/ stride);

编辑2:

也许有帮助,它是为了转换为System.Drawing.Bitmap.

4

1 回答 1

1

好的,我已经下载了库。完整的解决方案是:

byte[] bytes = new byte[imageSize * sizeof(int)];
int bytesInRow = width * sizeof(int);
//Invert bottom and top
for (int row = 0; row < height; row++)
    Buffer.BlockCopy(raster, row * bytesInRow, bytes, (height - row -1) * bytesInRow, bytesInRow);


//Invert R and B bytes
byte tmp;
for (int i = 0; i < bytes.Length; i += 4)
{
    tmp = bytes[i];
    bytes[i] = bytes[i + 2];
    bytes[i + 2] = tmp;
}

int stride = width * 4;
Image = BitmapSource.Create(
        width, height, 96, 96,
        PixelFormats.Pbgra32, null, bytes, stride);

解决方案有点复杂。实际上 WPF 不支持 rgba32 格式。因此,为了正确显示图像,应该交换 R 和 B 字节。另一个技巧是 tif 图像被颠倒加载。这需要一些额外的操作。

希望这可以帮助。

于 2014-04-15T17:24:16.810 回答