我正在使用 LibTiff.NET
读取多页 Tiff 文件。将我的 Tiff 转换成 System.Drawing.Bitmap
没问题,因为它显示在他们的网站上,但我想要的是 BitmapSource
或类似在 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:
也许 this有帮助,它用于转换为 System.Drawing.Bitmap
.
最佳答案
好的,我已经下载了库。完整的解决方案是:
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 图像是颠倒加载的。这需要一些额外的操作。
希望这对您有所帮助。
关于c# - LibTiff.NET Tiff 到 WPF 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23089432/