Azure:解码包含图片的base64中的字符串

时间:2016-07-14 10:00:51

标签: web-services azure base64 decode

我正在开发一个网络应用程序,必须从Azure实验生成的JSON中拍照。 我有以下功能。

public static Image Base64ToImage(string base64String)
{
   try
   {
      byte[] imageBytes = System.Text.Encoding.UTF8.GetBytes(base64String);//Convert.FromBase64String(base64String);
      MemoryStream ms1 = new MemoryStream(imageBytes,0,imageBytes.Length);
      ms1.Write(imageBytes, 0, imageBytes.Length);
      Image NewImage = Image.FromStream(ms1, true);
      NewImage.Save(@"C:\TESTOK.jpg");
      return NewImage;
   }
   catch (Exception ex)
   {
      string appo = ex.Message.ToString() + ex.InnerException.ToString();
      return null;
   }
}

它接收Azure webservice生成的字符串(编码图像)。当我尝试通过函数Image.FromStream(ms1, true);创建图像时,它会产生一个异常,返回错误:

  

"参数无效"。

输入字符串如下。

iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAMAAABKCk6nAAAADFBMVEX9/v0AAAAAzQD////A95lXAAAABHRSTlP/////bShGeQAAC+xJREFUeJztnYl22yoUAIP1///cprYQm3bZRtOZ856NEFyWsZZgNfkZBM3Ptzsg70XBcBQMR8FwFAxHwXAUDEfBcLoRHEKoUq/NNBFe5Lv2NLOrSNLYTMnNfSgHmI1ktanD9CQ45Im4I7odpmkJ467dzewpUkpolNwjOBtgyIOvNHWYGwmOW0N1lF/ck6RPi43t6kQtuNHG9eO6qeAjB+/2nmxt7ArBQ34yIguOJ+JJ6Sv1PHdl0xILvXYXExhiyHhkpBPZKhIza8FD3D9UJbM+jKVamooBVvcWRyduBYDgcW9+bZtSIc9bLpJfVUOerutkfWjdOs0OsBD8tovOLQQPqZup/Di5w/SaprLMmSpzRaZeJUdmfCm7Vre+X/Aw87k4C0DwMDT0lDHKalWR53spOGY2T9d1H5LQKwOsBE9NXUpPglO5JwQX59p45syqDVXt1im6aCtuVqfoRuhYsj3AluBy4wr+E8HZgTh3kGcRW20NSaRrBbdtX8FdBRe2qinOM1+bobJQ1tsmuOzarOCFAd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GRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHG
RxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRx3L2yELnclpkkAAAAASUVORK5CYII=

我试图从命令行解码图像:

$ base64 -d string > stringDecoded
base64: invalid input
$ base64 -d -i string > stringDecoded
$ 

其中参数-i代表 - ignore-garbage:*解码时忽略非字母字符。所以,这是要解决的问题。 对于Windows,您可以使用decb64执行相同的操作:

> dec64 stringa.txt

所以很明显问题是我的字符串包含非字母数字字符。如何修改我的代码以解决此问题?

1 个答案:

答案 0 :(得分:0)

我明白为什么没有人给我答案:没有答案。

几天前,AzureML开发人员修改了包含图像的JSON的结构。现在它看起来像这样:

enter image description here

知道为什么它不适用于以前的版本可能会很有趣。然而,问题解决了!