Asp.net将图片转为Base64编码

 1 protected void Page_Load(object sender, EventArgs e)

 2         {

 3             Image img = new Bitmap(Server.MapPath("/Libs/imgs/btn.png"));

 4             Response.Write("<img src='data:image/jpeg;base64," + ImageToBase(img, System.Drawing.Imaging.ImageFormat.Jpeg) + "' />");

 5         }

 6         public string ImageToBase(Image image, System.Drawing.Imaging.ImageFormat format)

 7         {

 8             using (MemoryStream ms = new MemoryStream())

 9             {

10                 // Convert Image to byte[]

11                 image.Save(ms, format);

12                 byte[] imageBytes = ms.ToArray();

13 

14                 // Convert byte[] to Base64 String

15                 string base64String = Convert.ToBase64String(imageBytes);

16                 return base64String;

17             }

18         }

 

你可能感兴趣的:(asp.net)