views:

258

answers:

1

Hi all,

I am sending a string formatted data of the image from an iPhone client code to a web service. I am trying to encode that in binary64 and then convert it to a byte array. I get this problem of Parameter not valid at the following point in the code.

        byte[] ImgInput = System.Text.Encoding.UTF8.GetBytes(ImgInputString);

        string imgString = Convert.ToBase64String(ImgInput);
        byte[] imgBYtes = Convert.FromBase64String(imgString);

        System.IO.Stream ms =(Stream)
        new System.IO.MemoryStream(ImgInput);
        //ms.Write(ImgInput, 0, ImgInput.Length);
        ImageConverter ic = new ImageConverter();

        Image image = (Image)ic.ConvertFrom(imgBYtes);---ERROR here
+2  A: 

The ImageConverter class isn't used to unpack image files. Use the Image.FromStream method to unpack the data in the array:

Image image;
using (MemoryStream m = new MemoryStream(imgBytes)) {
   image = Image.FromStream(m);
}

Edit:
However, your first problem is how you encode the data. You are getting a string, which you encode as UTF-8, then encode as base-64, then decode from base-64. What you have at that point is still a string encoded as UTF-8, and that is not something that you can load as an image. Converting the data to base-64 and back again doesn't change the data in any way.

If it is a base64 encoded string that you get as input, you should just decode it:

byte[] imgBytes = Convert.FromBase64String(ImgInputString);

If it's some other format, you have to decode it using the reverse process, to get the binary data that was encoded before it was sent.

Guffa
i already did that Guffaa..But i am getting same error but at the Memorystream object this time.....
jacob
@jacob: See my edit above.
Guffa