直接上代码
public class ImageHelper { /// <summary> /// 图片转Base64编号 /// </summary> /// <param name="imageFile"></param> /// <returns></returns> public static string ImageToBase64(string imageFile) { using (FileStream fs = new FileStream(imageFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { //Bitmap bp = new Bitmap(imageFile); //bp.Save(m, ImageFormat.Png); //byte[] b = m.GetBuffer(); //string base64String = Convert.ToBase64String(b); //m.Close(); //m.Dispose(); ////bp.Dispose(); //return base64String; byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); MemoryStream ms = new MemoryStream(bytes); Bitmap img = new Bitmap(ms); img.Save(ms,ImageFormat.Png); string base64String = Convert.ToBase64String(bytes); return base64String; } } /// <summary> /// Base64编号转为图片 /// </summary> /// <param name="base64String"></param> /// <param name="fileName"></param> /// <param name="format"></param> public static void Base64ToImage(string base64String, string fileName, ImageFormat format) { byte[] bt = Convert.FromBase64String(base64String); using (MemoryStream stream = new MemoryStream(bt)) { Bitmap bitmap = new Bitmap(stream); bitmap.Save(fileName, format); stream.Close(); stream.Dispose(); } } }