日期:2014-05-18 浏览次数:20923 次
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("图片路径"));