日期:2014-05-18  浏览次数:20943 次

C# 求图片转化为base64 字符串
读取图片,然后转化为base64 字符串
  是转化成字符串。不是写入文件。

------解决方案--------------------
binarystring , 有的数据库支持用二进制字符串对二进制字段进行插入, 更新
C# code

    string convertBinaryToHexString(byte[] bytes)
    {
      System.Text.StringBuilder sb = new StringBuilder();
      foreach (byte b in bytes)
      {
        sb.Append(b.ToString("X2"));
      }
      return sb.Insert(0, "0x").ToString();
    }

------解决方案--------------------
public string ImageToBase64String(Image imageData, ImageFormat format)
{
string base64;
MemoryStream memory = new MemoryStream();
imageData.Save(memory, format);
base64 = System.Convert.ToBase64String(memory.ToArray());
memory.Close();
memory = null;
return base64;
}

System.IO.MemoryStream m = new System.IO.MemoryStream();
System.Drawing.Bitmap bp = new System.Drawing.Bitmap(@“c:\a.GIF”);
bp.Save(m, System.Drawing.Imaging.ImageFormat.Gif);
byte[]b= m.GetBuffer();
string base64string=Convert.ToBase64String(b);

byte[] bt = Convert.FromBase64String(base64string);
System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
Bitmap bitmap = new Bitmap(stream);
pictureBox1.Image = bitmap;
------解决方案--------------------
使用.net 基类库,一行语句搞定:
var base64Img = Convert.ToBase64String(System.IO.File.ReadAllBytes("图片路径"));