Asp.net将图片转为Base64编码

 protected void Page_Load(object sender, EventArgs e)
{
Image img = new Bitmap(Server.MapPath("/Libs/imgs/btn.png"));
Response.Write("<img src='data:image/jpeg;base64," + ImageToBase(img, System.Drawing.Imaging.ImageFormat.Jpeg) + "' />");
}
public string ImageToBase(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;
}
}
上一篇:SPI and API


下一篇:Yii源码阅读笔记(八)