BitMap 图片格式与Base64Image格式互转方法
/// <summary> /// 图片转为base64编码的字符串 /// </summary> /// <param name="Imagefilename"></param> /// <returns></returns> protected string ImgToBase64String(string Imagefilename) { try { Bitmap bmp = new Bitmap(Imagefilename); 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; } } /// <summary> /// threeebase64编码的字符串转为图片 /// </summary> /// <param name="strbase64"></param> /// <returns></returns> protected Bitmap Base64StringToImage(string strbase64) { try { byte[] arr = Convert.FromBase64String(strbase64); MemoryStream ms = new MemoryStream(arr); Bitmap bmp = new Bitmap(ms); bmp.Save(@"d: est.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); //bmp.Save(@"d:"test.bmp", ImageFormat.Bmp); //bmp.Save(@"d:"test.gif", ImageFormat.Gif); //bmp.Save(@"d:"test.png", ImageFormat.Png); ms.Close(); return bmp; } catch (Exception ex) { return null; } }