/// <summary>
/// 图片转Base64
/// </summary>
/// <param name="bmp"></param>
/// <returns>Base64字符串</returns>
public static string BitmapToBase64String(Bitmap bmp)
{
try
{
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>
/// Base64转图片
/// </summary>
/// <param name="strbase64"></param>
/// <returns></returns>
public static Bitmap Base64StringToBitmap(string strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
return bmp;
}
catch (Exception ex)
{
return null;
}
}
/// 图片转Base64
/// </summary>
/// <param name="bmp"></param>
/// <returns>Base64字符串</returns>
public static string BitmapToBase64String(Bitmap bmp)
{
try
{
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>
/// Base64转图片
/// </summary>
/// <param name="strbase64"></param>
/// <returns></returns>
public static Bitmap Base64StringToBitmap(string strbase64)
{
try
{
byte[] arr = Convert.FromBase64String(strbase64);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
return bmp;
}
catch (Exception ex)
{
return null;
}
}