我有一个 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.
发布评论
评论(3)
好的,我发现了这个问题,它来自于与我所询问的代码部分无关的代码部分。 数据作为字符串传递,我将其转换为字节数组(这是一个测试设备,所以我试图模拟我在主应用程序中获得的字节数组),然后将其转换为 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 theImage.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.
所有这些线索都帮助我找出了我的问题,这与问题所提出的问题是同一个问题。 所以我想发布我由于这些有用的线索而得出的解决方案。 感谢迄今为止发布的所有线索!
正如 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!!
编辑:下面的假设不正确,我稍后有机会启动 IDE,并在使用和不使用 Write 的情况下进行测试,并且都正确填充了 MemoryStream。
我认为你需要先写入你的 MemeoryStream 。
好像我的记忆(没有双关语)正确地为我服务:
创建该大小的内存流。
然后,您需要将字节数组内容写入内存流:
看看是否可以修复它。
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:
Creates a memory stream of that size.
You then need to write your byte array contents to the memory stream:
See if that fixes it.