【发布时间】:2015-08-14 22:35:06
【问题描述】:
我正在尝试转换从画布元素调整大小的图像生成的 Base64 编码字符串,并且在使用 Convert.FromBase64() 进行转换时出现以下错误
输入不是有效的 Base-64 字符串,因为它包含非 base-64 字符、两个以上的填充字符或填充字符中的非法字符。
编码数据看起来像这样以 = 开头和结尾
data:image/jpeg;base64,/...=
我不太明白的是,当我执行 Convert.FromBase64() 时,我需要去掉 data:image/jpeg;base64, 的前缀,然后解码其余部分吗?
我用来解码的代码如下所示
string base64String = newinput.Value.ToString();
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
MemoryStream ms = new MemoryStream(imageBytes, 0, imageBytes.Length);
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
string newFile = Guid.NewGuid().ToString() + ".jpg";
image.Save(Path.Combine(Server.MapPath("~/Assets/") + Request.QueryString["id"] + "/", newFile), ImageFormat.Jpeg);
基本上,我将字符串转换为图像并保存在服务器上。如您所见,我使用的是 C#
有什么想法吗?
【问题讨论】:
-
明显的问题:你试过了吗?
标签: c# asp.net image base64 file-conversion