这个base64字符串没有被解码

时间:2016-10-18 13:28:22

标签: c# .net system.drawing

我将此图像表示为base64字符串,我已在此处粘贴: https://paste.ubuntu.com/23343680/ 里面有一个图像。我打电话给

Convert.FromBase64String 

它给了我

{"Invalid length for a Base-64 char array or string."}

现在我已经使用这个网站http://codebeautify.org/base64-to-image-converter来粘贴相同的字符串,它可以很好地呈现数据中的图像。 我在这里做错了什么,我需要得到一个字节[],我将从中制作图像,但我不能。 感谢。

2 个答案:

答案 0 :(得分:6)

你必须附加=,直到长度可被4整除。只有那是一个有效的base64字符串。

此代码将执行:

string s = "yourVeryLongBase64String";

if ((s.Length % 4) > 0)
{
    int diff = 4 - (s.Length % 4);
    s = s.PadRight(s.Length + diff, '=');
}

byte[] b = Convert.FromBase64String(s);

答案 1 :(得分:1)

string str = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAHgAoADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAV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/AJHo/wCHjP7Fn/RZ/wDy39Q/+R6/LiigD23/AIdy/tp/9EZ/8uDT/wD5Io/4dy/tp/8ARGf/AC4NP/8Akiv1HooA/Lj/AIdy/tp/9EZ/8uDT/wD5Io/4dy/tp/8ARGf/AC4NP/8Akiv1HooA/Lj/AIdy/tp/9EZ/8uDT/wD5Io/4dy/tp/8ARGf/AC4NP/8Akiv1HooA/Lj/AIdy/tp/9EZ/8uDT/wD5Io/4dy/tp/8ARGf/AC4NP/8Akiv1HooA8S/4eM/sWf8ARZ//AC39Q/8Akej/AIeM/sWf9Fn/APLf1D/5Hr8uKKAP1H/4eM/sWf8ARZ//AC39Q/8Akej/AIeM/sWf9Fn/APLf1D/5Hr8uKKAP1H/4eM/sWf8ARZ//AC39Q/8Akej/AIeM/sWf9Fn/APLf1D/5Hr8uKKAP1H/4eM/sWf8ARZ//AC39Q/8Akej/AIeM/sWf9Fn/APLf1D/5Hr8uKKAPbf8Ah3L+2n/0Rn/y4NP/APkij/h3L+2n/wBEZ/8ALg0//wCSK/UeigD8uP8Ah3L+2n/0Rn/y4NP/APkij/h3L+2n/wBEZ/8ALg0//wCSK/UeigD8uP8Ah3L+2n/0Rn/y4NP/APkij/h3L+2n/wBEZ/8ALg0//wCSK/UeigD8uP8Ah3L+2n/0Rn/y4NP/APkij/h3L+2n/wBEZ/8ALg0//wCSK/UeigDxL/h4z+xZ/wBFn/8ALf1D/wCR6P8Ah4z+xZ/0Wf8A8t/UP/kevy4ooAKKKKACiiigAooooA/bSiiigAooooAKKKKACiiigD8S6KKKACiiigAooooAKKKKAP20ooooAKKKKACiiigAooooA/EuiiigAooooAKKKKACiiigD9tKKKKACiiigAooooAKKKKAPxLooooAKKKKACiiigAooooA/bSiiigAooooAKKKKACiiigD8S6KKKACiiigAooooAKKKKAP20ooooAKKKKACiiigAooooA/EuiiigAooooAKKKKACiiigD9tKKKKACiiigAooooAKKKKAPxLooooAKKKKACiiigAooooA===";
            byte[] arr = Convert.FromBase64String(str);

在字符串末尾添加==。它会工作。您传递的输入不是有效的base64值