<p>I have a <code>byte[]</code> array, the contents of which represent a TIFF file (as in, if I write out these bytes directly to a file using the <code>BinaryWriter</code> 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)</p>

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

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

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

<pre><code>Parameter Not Valid
</code></pre>

<p>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 <code>FromStream</code> method accept this fact.</p>

<p>How do I turn a byte array of a TIFF image into an Image object?</p>

<p>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.</p>

[{'answer_id': 32841, 'author': 'Tim', 'author_id': 1970, 'author_profile': 'https://Stackoverflow.com/users/1970', 'pm_score': 3, 'selected': True, 'text': '<p><strong>Edit:</strong> 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.</p>\n\n<p>I think you need to write to your MemeoryStream first.</p>\n\n<p>As if my memory (no pun intended) serves me correctly this:</p>\n\n<pre><code>MemoryStream ms = new MemoryStream(byteArrayIn);\n</code></pre>\n\n<p>Creates a memory stream of that size.</p>\n\n<p>You then need to write your byte array contents to the memory stream:</p>\n\n<pre><code>ms.Write(byteArrayIn, 0, byteArrayIn.Length);\n</code></pre>\n\n<p>See if that fixes it.</p>\n'}, {'answer_id': 33101, 'author': 'Tom Kidd', 'author_id': 2577, 'author_profile': 'https://Stackoverflow.com/users/2577', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>What I failed to realize was that the string was Base64 encoded. Calling <code>Convert.FromBase64String()</code> caused it to turn into a byte array which wouldn\'t kill the <code>Image.FromStream()</code> method.</p>\n\n<p>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.</p>\n\n<p>Also, I found an easy way to construct a Multi-Page TIFF from my byte arrays <a href="http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-csharp/70308/Craete-tiff-Image-Page-by-Page" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 21838383, 'author': 'Shawn Kovac', 'author_id': 2840284, 'author_profile': 'https://Stackoverflow.com/users/2840284', 'pm_score': 1, 'selected': False, 'text': "<p>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!</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>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!</p>\n\n<p>Thanks Schnapple and Tim Saunders!!</p>\n"}]
['https://Stackoverflow.com/questions/32750', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2577/']