简体   繁体   English

LibTiff.NET Tiff到WPF图像

[英]LibTiff.NET Tiff to WPF Image

I'm using LibTiff.NET to read a multipage Tiff file . 我正在使用LibTiff.NET读取多页Tiff文件 It's no problem to convert my Tiff into a System.Drawing.Bitmap , since it's shown on their website, but what I want is a BitmapSource or something comparable to use in WPF . 将Tiff转换为System.Drawing.Bitmap没问题,因为它已显示在他们的网站上,但是我想要的是BitmapSource或可用于WPF类似产品。 Of course, I can convert the already converted System.Drawing.Bitmap , but since the amount of data is quite big, I am looking for a method to convert directly from the Tiff object . 当然,我可以转换已经converted System.Drawing.Bitmap ,但是由于数据量很大,我在寻找一种直接从Tiff object转换的方法。

Any suggestions? 有什么建议么? Maybe with the ReadRGBAImage method, that returns an int array with the colors? 也许使用ReadRGBAImage方法,该方法返回具有颜色的int数组?

Edit1: EDIT1:

I tried the following but only get an image consisting of gray stripes: 我尝试了以下操作,但只得到了由灰色条纹组成的图像:

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: EDIT2:

Maybe this helps, it is for conversion to System.Drawing.Bitmap . 也许会有所帮助,它是转化为System.Drawing.Bitmap

Ok I've downloaded the lib. 好的,我已经下载了该库。 The full solution is: 完整的解决方案是:

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);

The solution is a bit more complex. 解决方案有点复杂。 In fact WPF don't support rgba32 format. 实际上WPF不支持rgba32格式。 So to display the image correctly R and B bytes should be swapped. 因此,为了正确显示图像,应该交换R和B字节。 Another tric is that tif image is loaded upside down. 另一个指标是tif图像上下颠倒加载。 This needs some additional manipulation. 这需要一些额外的操作。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM