1
我正在開發一個Web應用程序,該應用程序必須從Azure實驗生成的JSON中拍攝圖片。 我有以下功能。Azure:解碼包含圖片的base64中的字符串
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);
它產生一個異常返回錯誤創建圖像:
"Parameter is not valid".
輸入字符串如下。
iVBORw0KGgoAAAANSUhEUgAAAeAAAAHgCAMAAABKCk6nAAAADFBMVEX9/v0AAAAAzQD////A95lXAAAABHRSTlP/////bShGeQAAC+xJREFUeJztnYl22yoUAIP1///cprYQm3bZRtOZ856NEFyWsZZgNfkZBM3Ptzsg70XBcBQMR8FwFAxHwXAUDEfBcLoRHEKoUq/NNBFe5Lv2NLOrSNLYTMnNfSgHmI1ktanD9CQ45Im4I7odpmkJ467dzewpUkpolNwjOBtgyIOvNHWYGwmOW0N1lF/ck6RPi43t6kQtuNHG9eO6qeAjB+/2nmxt7ArBQ34yIguOJ+JJ6Sv1PHdl0xILvXYXExhiyHhkpBPZKhIza8FD3D9UJbM+jKVamooBVvcWRyduBYDgcW9+bZtSIc9bLpJfVUOerutkfWjdOs0OsBD8tovOLQQPqZup/Di5w/SaprLMmSpzRaZeJUdmfCm7Vre+X/Aw87k4C0DwMDT0lDHKalWR53spOGY2T9d1H5LQKwOsBE9NXUpPglO5JwQX59p45syqDVXt1im6aCtuVqfoRuhYsj3AluBy4wr+E8HZgTh3kGcRW20NSaRrBbdtX8FdBRe2qinOM1+bobJQ1tsmuOzarOCFAdYjma15iq4ED3/HP2RK5wS3bo7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RxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHG
RxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRxncJzBcQbHGRx3L2yELnclpkkAAAAASUVORK5CYII=
我試圖將圖像從命令行解碼:
$ base64 -d string > stringDecoded
base64: invalid input
$ base64 -d -i string > stringDecoded
$
其中-i代表- 忽略垃圾的參數:·解碼時,忽略非字母字符 。所以,這是要解決的問題。 對於Windows,你可以使用decb64做同樣的事情:
> dec64 stringa.txt
所以似乎很明顯的問題是,我的字符串包含非字母數字字符。我如何修改我的代碼來解決這個問題?