将base 64字符串转换为图像并保存这是我的代码:protected void SaveMyImage_Click(object sender, EventArgs e)
{
string imageUrl = Hidden1.Value;
string saveLocation = Server.MapPath("~/PictureUploads/whatever2.png") ;
HttpWebRequest imageRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
WebResponse imageResponse = imageRequest.GetResponse();
Stream responseStream = imageResponse.GetResponseStream();
using (BinaryReader br = new BinaryReader(responseStream))
{
imageBytes = br.ReadBytes(500000);
br.Close();
}
responseStream.Close();
imageResponse.Close();
FileStream fs = new FileStream(saveLocation, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
try
{
bw.Write(imageBytes);
}
finally
{
fs.Close();
bw.Close();
}
}}顶部imageUrl声明采用Base64图像字符串,我想将其转换为图像。我认为我的代码集仅适用于像“www.mysite.com/test.jpg”这样的图像而不适用于Base64字符串。有人有什么建议吗?谢谢!
3 回答
萧十郎
TA贡献1815条经验 获得超13个赞
下面是一个示例,您可以修改方法以接受字符串参数。然后用.Save()保存图像对象。
public Image LoadImage(){ //data:image/gif;base64, //this image is a single pixel (black) byte[] bytes = Convert.FromBase64String("R0lGODlhAQABAIAAAAAAAAAAACH5BAAAAAAALAAAAAABAAEAAAICTAEAOw=="); Image image; using (MemoryStream ms = new MemoryStream(bytes)) { image = Image.FromStream(ms); } return image;}
A generic error occurred in GDI+.
当字节表示位图时,可能会出现异常。如果发生这种情况,请在处理内存流之前保存图像(同时仍在using语句中)。
- 3 回答
- 0 关注
- 447 浏览
添加回答
举报
0/150
提交
取消