首页 > 代码库 > base64和图片的转换

base64和图片的转换

/// <summary>
/// base64转图片
/// </summary>
/// <param name="strBase64"></param>
/// <param name="pathName"></param>
public static void Base64StringToImage(string strBase64,string pathName )
{
String inputStr = strBase64;
byte[] arr = Convert.FromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);

bmp.Save(pathName, ImageFormat.Png);
ms.Close();
}
/// <summary>
/// 图片转base64
/// </summary>
/// <param name="Imagefilename"></param>
/// <returns></returns>
//图片 转为 base64编码的文本
public static string ImgToBase64String(string Imagefilename)
{
Bitmap bmp = new Bitmap(Imagefilename);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
String strbaser64 = Convert.ToBase64String(arr);
return strbaser64;
}

base64和图片的转换