Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

image

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.

like image 584
Tom Kidd Avatar asked Aug 28 '08 16:08

Tom Kidd


People also ask

How can I get byte array from image?

Read the image using the read() method of the ImageIO class. Create a ByteArrayOutputStream object. Write the image to the ByteArrayOutputStream object created above using the write() method of the ImageIO class. Finally convert the contents of the ByteArrayOutputStream to a byte array using the toByteArray() method.

How do I extract an image from a TIFF?

Open the tiff file in Adobe Acrobat Professional 7.0 (for large files this will take some time). Pull down the Acrobat menu and select Preferences. Click on the “Picture Tasks” button and select Export Pictures.

Can you convert TIFF files?

Make your TIFF images into JPGs.Choose File and select Save As. Or, choose File, then Export, and Save for Web (Legacy). Either process can be used to save CMYK, RGB, or grayscale images.


2 Answers

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.

like image 118
Tom Kidd Avatar answered Sep 25 '22 14:09

Tom Kidd


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.

like image 33
Tim Avatar answered Sep 22 '22 14:09

Tim