来自base64字符串的图像,它是Application / Octet-stream的类型 [英] Image from base64 string which is type of Application/Octet-stream
本文介绍了来自base64字符串的图像,它是Application / Octet-stream的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个base64字符串,如下所示。我猜它的类型是Application / Octet-stream。
我使用下面的代码将其转换为图像。但它给了我错误Paramter无效!。
I have a base64 string which is provided below. I guess its type is Application/Octet-stream.
I am using code below to convert it to an image. But it gives me error "Paramter is not valid!".
string base64string = "AAAAAQEAAAEcEgABHxIA ... xYAAF4WQABcFwAAQ==";
using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(base64string)))
{
pictureEdit1.Image = Image.FromStream(ms, true);
}
你可以从这里得到完整的字符串:http://orion.mn/updateapp/base64_unicode_string.rar [ ^ ]!
请帮助我如何将其转换为图像?
You can get full string from here: http://orion.mn/updateapp/base64_unicode_string.rar[^]!
Please, help me how to convert it to an image?
推荐答案
我认为你的64字符串不正确。你是如何将它转换为64的?
您可以像这样验证Base 64字符串:
Hi,
I think your base 64 string is incorrect. How did you convert it to base 64 ?
You can validate your Base 64 string like this:
public bool IsBase64String(string s)
{
s = s.Trim();
return (s.Length % 4 == 0) && Regex.IsMatch(s, @"^[a-zA-Z0-9\+/]*={0,3}
,RegexOptions.None);
}
", RegexOptions.None); }
然后你可以这样称呼:
Then you can call it like this:
public void LoadImage()
{
string string64 = "R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw==";
bool isStringValid = this.IsBase64String(string64);
if (isStringValid)
{
byte[] bytes = Convert.FromBase64String(string64);
System.Drawing.Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = System.Drawing.Image.FromStream(ms);
//image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
}
}
}
这篇关于来自base64字符串的图像,它是Application / Octet-stream的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文