C# LibTiff.NET Tiff到WPF图像
我正在使用C# LibTiff.NET Tiff到WPF图像,c#,wpf,libtiff.net,C#,Wpf,Libtiff.net,我正在使用LibTiff.NET读取一个多页Tiff文件。将我的Tiff转换成System.Drawing.Bitmap没有问题,因为它显示在他们的网站上,但我想要的是BitmapSource或类似于WPF中使用的东西。 当然,我可以转换已转换的System.Drawing.Bitmap,但由于数据量很大,我正在寻找一种直接从Tiff对象转换的方法 有什么建议吗?也许使用ReadRGBAImage方法,它返回一个带有颜色的int数组 编辑1: 我尝试了以下方法,但只得到了由灰色条纹组成的图像:
LibTiff.NET
读取一个多页Tiff文件。将我的Tiff转换成System.Drawing.Bitmap
没有问题,因为它显示在他们的网站上,但我想要的是BitmapSource
或类似于WPF
中使用的东西。
当然,我可以转换已转换的System.Drawing.Bitmap,但由于数据量很大,我正在寻找一种直接从Tiff对象转换的方法
有什么建议吗?也许使用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:
也许有帮助,它是用来转换成系统.Drawing.Bitmap
好的,我已经下载了lib。完整的解决方案是:
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);
byte[]bytes=新字节[imageSize*sizeof(int)];
int bytesInRow=宽度*sizeof(int);
//倒置底部和顶部
对于(int row=0;row
解决方案有点复杂。事实上WPF不支持rgba32格式。因此,为了正确显示图像,应该交换R和B字节。另一个原因是tif图像的加载方式是颠倒的。这需要一些额外的操作
希望这有帮助。您可以将int数组转换为字节,然后使用BitmapSource。字节[]结果=新字节[intArray.Length*sizeof(int)];块复制(intArray,0,result,0,result.Length);经过一点实验,我得到了一个损坏的图像。只有一些灰色条纹。使用不同的像素格式进行测试。它应该是类似Rgba32的东西。它是一张黑白照片。。。我想应该只有两种颜色。在System.Drawing中,它的System.Drawing.Imaging.PixelFormat.Format1BPindexed(正确显示)良好如果光栅为int格式,则每个像素将被读取为32位值。实际上,doc说它可以在32位rgba中转换任何格式的图片。