Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and save an image from a byte[] causes Parameter is not valid exception

I have implemented the following functionality that connects to webservice and downloads a favicon from a given site and saves it to a byte[] which I store in our database. I now want to set it up so that it saves the icon to the disk. However I am getting a "Parameter is not valid" when I try and create the image from the byte[].

My code is as follows..

stream.Write(imageByteArray, 0, imageByteArray.Length);
Image i = Image.FromStream(stream); // EXCEPTION HAPPENS HERE.
i.Save(@"C:\tmp\" + filename + ".ico");

The exception occurs on the middle line.

This code works perfectly 9 times out of ten, but for some favicons, even thought the icon is a valid image (or at least it appears to be and it shows in the browser when point at it) I get this exception.

Does anyone have any ideas? I am pulling my hair out here!

Thanks

Dave

Edit: The value in the array that appears to throw the error is 127.

like image 472
Dave Avatar asked Nov 18 '10 16:11

Dave


People also ask

What happens if the image parameter is not valid?

But - if the data is not a valid image for any reason, the line: will throw an exception: "Parameter is not valid." It is only if you actually look at the data returned from the database that you realise why - and it's not so obvious when you glance at it in the debugger:

Why does the image class throw an exception when converting data?

In short, the data you read from the database is a human readable string, that says When the Image class tries to convert it to an actual image, it can't understand it at all, and throws the exception. The code works - it's the data that is the problem.

Why can't I create a human readable image from a string?

Normally, this is as a result of concatenating strings to form an SQL command: Does not include the image content in the SQL command - it calls the default Image.ToString method, which returns the human readable Image type: Which will not cause an error - but it will save the name of the image type as what you think is the Image data itself.

Is it possible to read a 21 byte image?

It doesn't look like anything you recognise, so it could be your data - though the length of 21 bytes is a big clue: is your image likely to be only 21 bytes long? That's a pretty small image... But it is readable, with a little practice.


2 Answers

There's no need to put it into an image, just spit the bytes straight out:

var fs = new BinaryWriter(new FileStream(@"C:\\tmp\\" + filename + ".ico", FileMode.Append, FileAccess.Write));
fs.Write(imageByteArray);
fs.Close();
like image 113
Julia Hayward Avatar answered Nov 04 '22 18:11

Julia Hayward


I knew you had the answer you need but I just want to go on your original idea. I think the problem is your byte array somehow had been changed and become byte char array, you just need to add this code to make it become byte array again:

for (int i=0;i<imageByteArray.Length;i++)
{
    imageByteArray[i]=(byte) imageByteArray[i];
}

I had this problem and solved it by this solution. Good luck!

like image 39
Khoi Avatar answered Nov 04 '22 17:11

Khoi