c# - LibTiff.NET Tiff 到 WPF 图像

标签 c# wpf libtiff.net

我正在使用 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/

相关文章:

c# - 从 C# 使用 LibTiff(访问平铺的 TIFF 图像)

c# - wpf渲染同步

c# - 内存未在 WPF Image 中释放

c# - 使模型从 Microsoft.Practices.Prism.ViewModel 命名空间中的 NotificationObject 继承是否合理?

c# - 如何判断 DataGrid 的哪一行被单击?

c# - 使用 LibTiff.Net 重复目录更新现有图像中的标签

.net - 从多页 TIFF 文件中删除/删除页面

c# - 自定义工具错误 : after switching project from .net 4.5 到 .Net 4.6.1

c# - 为什么我的 ASP.NET MVC 路由中有查询字符串?

C# 以编程方式创建 xyseries