Hadrien Moreno Hadrien Moreno - 1 year ago 142
C# Question

C# byte[] to bitmap System.InvalidOperationException

I'm doing a C# web service soap receiving an image.
I send a string contain the byte characters.
I transform the string in byte[] and next I world like to create the Bitmap.

The line

Bitmap img = new Bitmap(ms);
generate an exception : invalid argument.
I have a in the ms object this error : System.InvalidOperationException

value contain the correct string, imgBytes contain the good number of sell.

public string GetImage(string value)
byte[] imgBytes = Encoding.ASCII.GetBytes(value);

MemoryStream ms = new MemoryStream(imgBytes, true);
Bitmap img = new Bitmap(ms);

Code with debug mode


Thank you for your help.

Answer Source

It looks like your string holds base64 encoded data. Try to decode it to a byte array via Convert.FromBase64String

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download