图片和字节流之间的转换
将图片保存成byte数组保存到数据库,将byte数组转为图片
/// <summary>
/// 把一幅图片转换成字节数组
/// </summary>
/// <param name="imageData"></param>
/// <returns></returns>
private byte[] ToByte(Image imageData)
{
MemoryStream ms = new MemoryStream();
imageData.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);//把图像数据序列化到内存
byte[] imgByte = new byte[ms.Length];
ms.Position = 0;
ms.Read(imgByte, 0, Convert.ToInt32(ms.Length));
ms.Close();
return imgByte;
}
/// <summary>
/// 把获得数据流转为一副图片
/// </summary>
/// <param name="imageDataDetails"></param>
/// <returns></returns>
private Bitmap AddHeader(byte[] imageDataDetails)
{
if (imageDataDetails == null)
{
return null;
}
MemoryStream stream = new MemoryStream(imageDataDetails);
System.Drawing.Bitmap result = new Bitmap(stream);
return result;
}
文章来源: wanghao.blog.csdn.net,作者:AI浩,版权归原作者所有,如需转载,请联系作者。
原文链接:wanghao.blog.csdn.net/article/details/106207551
- 点赞
- 收藏
- 关注作者
评论(0)