图片和Base64之间的转换

时间:2024-01-15 21:25:32

public static Bitmap GetImageFromBase64String(string strBase)
{
  try
  {
    MemoryStream stream = new MemoryStream(Convert.FromBase64String(strBase));
    Bitmap bitmap = new Bitmap(stream);
    return bitmap;
  }
  catch(Exception ex)
  {
  return null;
  }

}

public static string GetBase64StringFromImage(Image image, ImageFormat imageFormat)
{
  string strRst = string.Empty;
  try
  {
    MemoryStream stream = new MemoryStream();
    image.Save(stream, imageFormat);
    strRst = Convert.ToBase64String(stream.GetBuffer());
  }
  catch (Exception ex)
  {}
return strRst;
}