首页 > 代码库 > Image 和byte[]之间的转换
Image 和byte[]之间的转换
1.Image 转 byte[]
public byte[] GetByteByImage(Image image)
{
byte[] bt = null;
try
{
if (!image.Equals(null))
{
MemoryStream ms = new MemoryStream();
Bitmap bmp = new Bitmap(image);
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
bt = new byte[ms.Length];
ms.Position = 0;
ms.Read(bt, 0, Convert.ToInt32(bt.Length));
bmp.Dispose();
ms.Dispose();
ms.Close();
}
}
catch (Exception ex)
{
throw ex;
}
return bt;
}
2.byte[] 转Image
public Image GetImageByByte(byte[] imageBytes)
{
Image image = null;
try
{
MemoryStream ms = new MemoryStream(imageBytes);
ms.Write(imageBytes, 0, imageBytes.Length);
image = Image.FromStream(ms, true);
ms.Dispose();
ms.Close();
}
catch (Exception ex)
{
throw ex;
}
return image;
}
Image 和byte[]之间的转换