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

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


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();


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!


Add image format:

stream.Position = 0;i.Save(@"C:\tmp\" + filename + ".ico", System.Drawing.Imaging.ImageFormat.Icon);