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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 09:13:08  来源:igfitidea点击:

How can I take a byte array of a TIFF image and turn it into a System.Drawing.Image object?

提问by Tom Kidd

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 BinaryWriterobject, 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)

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

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.FromStreammethod dies at runtime, saying

对我不起作用。上述方法的第二行调用该Image.FromStream方法在运行时死亡,说

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 FromStreammethod accept this fact.

我相信该方法因为这是一个 TIFF 文件而令人窒息,但我无法弄清楚如何使该FromStream方法接受这个事实。

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

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

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.

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

采纳答案by Tim Saunders

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.

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

I think you need to write to your MemeoryStream first.

我认为您需要先写入 MemeoryStream。

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.

看看能不能解决

回答by Tom Kidd

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.

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

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.

我没有意识到该字符串是 Base64 编码的。调用Convert.FromBase64String()导致它变成一个不会终止该Image.FromStream()方法的字节数组。

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.

另外,我发现了一个简单的方法来从我的字节数组构造一个多页TIFF这里

回答by Shawn Kovac

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.

正如 Time Saunders 在他的回答中发布的那样,将字节实际写入内存流的 Write 方法是必不可少的。那是我的第一个错误。

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.

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

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.

当我阅读一些基本的 TIFF 文件格式规范时,我发现 TIFF 文件必须以 II 或 MM 开头(两个字节,值为 73 或 77)。II 表示使用小端字节顺序('Intel 字节顺序')。MM 表示使用大结尾('摩托罗拉字节顺序')。接下来的两个字节是 42 的两字节整数值(= Int16 in .NET),二进制 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!

因此,正确的 TIFF 字节流以十进制字节值开始:73, 73, 42, 0 或 77, 77, 0, 42。确保您的数据是有效的 TIFF 数据!

Thanks Schnapple and Tim Saunders!!

感谢 Schnapple 和 Tim Saunders !!