如何获取 TIFF 图像的字节数组并将其转换为 System.Drawing.Image 对象?

发布于 2024-07-05 04:34:34 字数 746 浏览 6 评论 0 原文

我有一个 byte[] 数组,其内容代表一个 TIFF 文件(例如,如果我使用 BinaryWriter 对象将这些字节直接写到文件中,它形成一个完全有效的 TIFF 文件),我试图将其转换为 System.Drawing.Image 对象,以便我可以将其用于以后的操作(馈入多页 TIFF 对象)

我遇到的问题是,通常此任务接受的代码:

    public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms, true);
        return returnImage;
    }

对我不起作用。 上述方法的第二行调用 Image.FromStream 方法在运行时终止,表示

Parameter Not Valid

我相信该方法因这是一个 TIFF 文件而令人窒息,但我不知道如何使 FromStream 方法接受这一事实。

如何将 TIFF 图像的字节数组转换为 Image 对象?

另外,正如我所说,最终目标是拥有一个表示多页 TIFF 文件的字节数组,其中包含我现在拥有字节数组对象的 TIFF 文件。 如果有更好的方法来做到这一点,我完全赞成。

I have a byte[] array, the contents of which represent a TIFF file (as in, if I write out these bytes directly to a file using the BinaryWriter object, it forms a perfectly valid TIFF file) and I'm trying to turn it into a System.Drawing.Image object so that I can use it for later manipulation (feeding into a multipage TIFF object)

The problem I'm having is that the commonly accepted code for this task:

    public Image byteArrayToImage(byte[] byteArrayIn)
    {
        MemoryStream ms = new MemoryStream(byteArrayIn);
        Image returnImage = Image.FromStream(ms, true);
        return returnImage;
    }

doesn't work for me. The second line of the above method where it calls the Image.FromStream method dies at runtime, saying

Parameter Not Valid

I believe that the method is choking on the fact that this is a TIFF file but I cannot figure out how to make the FromStream method accept this fact.

How do I turn a byte array of a TIFF image into an Image object?

Also, like I said the end goal of this is to have a byte array representing a multipage TIFF file, which contains the TIFF files for which I have byte array objects of right now. If there's a much better way to go about doing this, I'm all for it.

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

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

发布评论

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

评论(3

一世旳自豪 2024-07-12 04:34:34

好的,我发现了这个问题,它来自于与我所询问的代码部分无关的代码部分。 数据作为字符串传递,我将其转换为字节数组(这是一个测试设备,所以我试图模拟我在主应用程序中获得的字节数组),然后将其转换为 MemoryStream,然后进行从中得到的图像。

我没有意识到该字符串是 Base64 编码的。 调用 Convert.FromBase64String() 会导致其转换为字节数组,而不会终止 Image.FromStream() 方法。

所以基本上这可以归结为我的一个愚蠢的错误。 但是,嘿,上面的代码仍然有用,这个页面可能会作为谷歌结果,告诉其他人如何避免这个错误。

另外,我找到了一种从字节数组构建多页 TIFF 的简单方法 此处

OK, I found the issue, and it was from a part of the code unrelated to the part of the code I was asking about. The data was being passed as a string, I was converting it to a byte array (this was a test rig so I was trying to simulate the byte array that I get in the main app), then converting that to a MemoryStream, then making an Image from that.

What I failed to realize was that the string was Base64 encoded. Calling Convert.FromBase64String() caused it to turn into a byte array which wouldn't kill the Image.FromStream() method.

So basically it boiled down to a stupid mistake on my part. But hey, the code above is still useful and this page will probably serve as a Google result as to how to avoid this mistake to someone else.

Also, I found an easy way to construct a Multi-Page TIFF from my byte arrays here.

乖乖兔^ω^ 2024-07-12 04:34:34

所有这些线索都帮助我找出了我的问题,这与问题所提出的问题是同一个问题。 所以我想发布我由于这些有用的线索而得出的解决方案。 感谢迄今为止发布的所有线索!

正如 Time Saunders 在他的回答中所发表的那样,将字节实际写入内存流的 Write 方法至关重要。 这是我的第一个错误。

然后我的数据也是坏的 TIFF 数据,但就我而言,我的图像数据开头有一个额外的字符 13。 一旦我删除了它,一切对我来说都很好。

当我阅读一些基本的 TIFF 文件格式规范时,我发现 TIFF 文件必须以 II 或 MM 开头(两个字节,值为 73 或 77)。 II 表示使用小端字节顺序(“英特尔字节顺序”)。 MM 表示使用大尾(“摩托罗拉字节排序”)。 接下来的两个字节是一个两字节整数值(= .NET 中的 Int16)42,二进制 101010。

因此,正确的 TIFF 字节流以十进制字节值开始:73、73、42、0 或 77、77、 0, 42。我鼓励任何与我们遇到同样问题的人检查您的 TIFF 数据字节流并确保您的数据是有效的 TIFF 数据!

谢谢施纳普尔和蒂姆桑德斯!!

All these were clues that helped me figure out my problem which was the same problem as the question asks. So i want to post my solution which i arrived at because of these helpful clues. Thanks for all the clues posted so far!

As Time Saunders posted in his answer, that Write method to actually write the bytes to the memory stream is essential. That was my first mistake.

Then my data was bad TIFF data too, but in my case, i had an extra character 13 at the beginning of my image data. Once i removed that, it all worked fine for me.

When i read about some basic TIFF file format specs, i found that TIFF files must begin with II or MM (two bytes with values of either 73 or 77). II means little-endian byte order ('Intel byte ordering') is used. MM means big-ending ('Motorola byte ordering') is used. The next two bytes are a two byte integer value ( = Int16 in .NET) of 42, binary 101010.

Thus a correct TIFF stream of bytes begins with the decimal byte values of: 73, 73, 42, 0 or 77, 77, 0, 42. I encourage anyone with the same problem that we experienced to inspect your TIFF data byte stream and make sure your data is valid TIFF data!

Thanks Schnapple and Tim Saunders!!

初懵 2024-07-12 04:34:34

编辑:下面的假设不正确,我稍后有机会启动 IDE,并在使用和不使用 Write 的情况下进行测试,并且都正确填充了 MemoryStream。

我认为你需要先写入你的 MemeoryStream 。

好像我的记忆(没有双关语)正确地为我服务:

MemoryStream ms = new MemoryStream(byteArrayIn);

创建该大小的内存流。

然后,您需要将字节数组内容写入内存流:

ms.Write(byteArrayIn, 0, byteArrayIn.Length);

看看是否可以修复它。

Edit: The assumption below is not correct, I had a chance to fire up my IDE later and tested with and without Write and both populated the MemoryStream correctly.

I think you need to write to your MemeoryStream first.

As if my memory (no pun intended) serves me correctly this:

MemoryStream ms = new MemoryStream(byteArrayIn);

Creates a memory stream of that size.

You then need to write your byte array contents to the memory stream:

ms.Write(byteArrayIn, 0, byteArrayIn.Length);

See if that fixes it.

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