zoukankan      html  css  js  c++  java
  • C# Bitmap生成base64码

    public static string ImgToBase64String(Bitmap bmp)
            {
                try
                {
                    MemoryStream ms = new MemoryStream();
                    bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    byte[] arr = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(arr, 0, (int)ms.Length);
                    ms.Close();
                    return Convert.ToBase64String(arr);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }

    只需要传入一个Bitmap对象,亲测可用哈。返回给前台时,将转换成base64的码前面加上

    data:image/png;base64,

    例如:

    Bitmap bt;
                string base64Img = ImgToBase64String(bt);
                this.Image1.ImageUrl = "data:image/png;base64," + base64Img;
  • 相关阅读:
    9-15
    9-5
    8-26
    8-24
    7-20
    7-17
    我离职后要干些什么
    6-18
    5-28
    5-20
  • 原文地址:https://www.cnblogs.com/LoveQin/p/10576906.html
Copyright © 2011-2022 走看看