无法使用 .Net 图像或位图类打开带有颜色的 .tif

发布于 2024-08-25 15:30:39 字数 334 浏览 6 评论 0原文

我正在尝试使用 .Net Image 和 Bitmap 类打开有颜色的 .tif 文件(300 dpi,PixelFormat.Format24bppRgb)。当 tiff 有颜色时,我总是收到“无效参数”错误(对于黑白 tiff 效果很好)。如果有人有关于如何打开带有颜色的 .tif 的源代码,我将非常感激。以下是我正在尝试做的事情;调用 Bitmap.FromStream 时也会失败:

using (FileStream fs = File.OpenRead(fileName))
{
    using (Image img = Image.FromStream(fs)) {}
}

I am attempting to open .tif files that have color in them (300 dpi, PixelFormat.Format24bppRgb) using the .Net Image and Bitmap classes. I always get an "invalid parameter" error when the tiffs have color (works fine for black and white tiffs). If anyone has source code on how to open a .tif with color in it I'd deeply appreciate it. Below is what I'm attempting to do; this also fails when calling Bitmap.FromStream:

using (FileStream fs = File.OpenRead(fileName))
{
    using (Image img = Image.FromStream(fs)) {}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

场罚期间 2024-09-01 15:30:39

无法打开图像文件的原因是图像可能具有压缩格式。
当我打开 .gif 图像文件时,我也遇到了这个问题。
我正在寻找解决方案。
我们可以互相交流。
[电子邮件受保护]
谢谢

The reason why you can't open the image file is that the image maybe has compression format.
I also encounter this problem when I open a .gif image file.
I'm searching the solutions.
We can communicate each other.
[email protected]
thanks

行至春深 2024-09-01 15:30:39

我认为基本问题是无法使用 .Net Image/Bitmap 类可靠地打开彩色 TIFF。许多用于编码彩色 TIFF 的各种压缩格式都破坏了 .Net 图像/位图类。

这似乎是第 22 条军规——你必须知道 TIFF 格式信息才能知道如何加载它,但在 .Net 中需要加载 Image/Bitmap 类才能读取 TIFF 格式信息。

我认为我的问题的答案是,“这不能仅使用 .Net 框架来完成”,并且我需要购买第 3 方控件或自己用 C++ 创建一些东西。

I think the basic problem is that one cannot use the .Net Image/Bitmap classes to reliably open color TIFFs. Many of the various compression formats used to encode color TIFFs break the .Net Image/Bitmap class.

It seems to be a catch 22--you have to know the TIFF formatting information to know how to load it, but in .Net one needs to load the Image/Bitmap class to read TIFF formatting information.

I think the answer to my question is, "this can't be done just using the .Net framework," and that I'll need to either buy a 3rd party control or create something myself in C++.

这个俗人 2024-09-01 15:30:39

我发现 GDI(+) 是处理 TIFF 的限制因素。我在 XP 上遇到了许多“无效参数”问题(在我的例子中是因为使用 16bpp 灰度和平铺 tiff)。相同的代码在 Windows7/64 位上完美运行。

对我来说,免费的 LibTIFF.NET 就是解决方案。 (StackOverflow 使用 c# 中的 LibTIFF)仅比较 con到 GDI+:您必须滚动自己的 BMP(未压缩字节数组到 BMP 转换)。如果您愿意,我可以为您提供示例。

要诊断您的 TIFF 文件类型,您可以使用 TiffDump 下载链接
使用 tiffdump myfile.tif 会产生如下输出:

Magic: 0x4949 <little-endian> Version: 0x2a
Directory 0: offset 1669857288 (0x63880008) next 0 (0)
ImageWidth (256) SHORT (3) 1<33227>
ImageLength (257) SHORT (3) 1<24986>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<1>
FillOrder (266) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
XResolution (282) RATIONAL (5) 1<6400>
YResolution (283) RATIONAL (5) 1<6400>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<2>
TileWidth (322) SHORT (3) 1<256>
TileLength (323) SHORT (3) 1<256>

最有可能的是,此类信息可以在 StackOverflow 上为您提供更多具体答案。

I found the GDI(+) the limiting factor in dealing with TIFF. I had many "invalid parameter" issues on XP (in my case because of using 16bpp grayscale and tiled tiff). The same code worked flawlessly on Windows7/64bit.

For me, the free LibTIFF.NET was the solution. (StackOverflow using LibTIFF from c#) Only con compared to GDI+: you'll have to roll your own BMP (uncompressed byte array to bmp conversion). I can provide you with examples if you like.

To diagnose your TIFF file type, you could use TiffDump download link
Using like tiffdump myfile.tif will produce an output like:

Magic: 0x4949 <little-endian> Version: 0x2a
Directory 0: offset 1669857288 (0x63880008) next 0 (0)
ImageWidth (256) SHORT (3) 1<33227>
ImageLength (257) SHORT (3) 1<24986>
BitsPerSample (258) SHORT (3) 1<16>
Compression (259) SHORT (3) 1<1>
Photometric (262) SHORT (3) 1<1>
FillOrder (266) SHORT (3) 1<1>
SamplesPerPixel (277) SHORT (3) 1<1>
XResolution (282) RATIONAL (5) 1<6400>
YResolution (283) RATIONAL (5) 1<6400>
PlanarConfig (284) SHORT (3) 1<1>
ResolutionUnit (296) SHORT (3) 1<2>
TileWidth (322) SHORT (3) 1<256>
TileLength (323) SHORT (3) 1<256>

Most likely such info can get you more specifc answers on StackOverflow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文