public string ImageToBase64(Image image,
System.Drawing.Imaging.ImageFormat format)
{
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
image.Save(ms, format);
byte[] imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}
}
public Image Base64ToImage(string base64String)
{
// Convert Base64 String to byte[]
byte[] imageBytes = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
{
// Convert byte[] to Image
ms.Write(imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream(ms, true);
return image;
}
}
@HardLuck ...看起來不錯,在第二種方法中,不應該在使用塊中初始化MemoryStream以確保它正確處置? – Paul 2013-02-26 12:48:17
你說得對,現在更正確。 – HardLuck 2013-02-26 12:57:35
@hardluck如何將圖像存儲到應用程序中的指定位置說應用程序的資產文件夾? – Justice 2013-02-26 13:23:47