将 Base64 字符串转换为图像并保存
使用 Base64 编码的图像时,将它们转换为图像可能具有挑战性实际图像文件。为了解决这个问题,让我们探索一个修改后的代码片段,它可以有效地将 Base64 字符串转换为图像并将其保存以进行存储:
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(); byte[] imageBytes; using (var br = new BinaryReader(responseStream)) { var imageString = br.ReadString(); imageBytes = Convert.FromBase64String(imageString); } responseStream.Close(); imageResponse.Close(); Image image = Image.FromStream(new MemoryStream(imageBytes)); FileStream fs = new FileStream(saveLocation, FileMode.Create); image.Save(fs, ImageFormat.Png); fs.Close(); }
在此修改后的代码中:
以上是如何将 Base64 字符串转换为图像并保存?的详细内容。更多信息请关注PHP中文网其他相关文章!