我使用LibTiff.NET读取多页Tiff文件。将我的Tiff转换成System.Drawing.Bitmap是没有问题的,因为它显示在他们的网站上,但是我想要的是一个BitmapSource或者类似于WPF的东西。当然,我可以转换已经存在的converted System.Drawing.Bitmap,但是由于数据量相当大,我正在寻找一种直接从Tiff object转换的方法。
有什么建议吗?也许使用ReadRGBAImage方法,返回一个带有颜色的int数组?
Edit1:
我尝试了以下方法,但只得到了一幅由灰色条纹组成的图像:
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);Edit2:
也许这会有所帮助,它是用于转换为System.Drawing.Bitmap的。
发布于 2014-04-15 17:24:16
好的,我下载了库。全面的解决办法是:
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图像是倒装的。这需要一些额外的操作。
希望这能有所帮助。
https://stackoverflow.com/questions/23089432
复制相似问题