开发者

ImageConverter: Parameter is not valid for a byte array

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.

 开发者_StackOverflow       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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜