C# · 12月 25, 2021

C# 图片与二进制转换的简单实例

@H_419_1@复制代码 代码如下:
///图片转二进制
private byte[] convertByte(Image img)
{
MemoryStream ms = new MemoryStream();
img.Save(ms,img.RawFormat);
//byte[] bytes = new byte[ms.Length];
//ms.Read(bytes,Convert.ToInt32(ms.Length));
//以上两句改成下面两句
byte[] bytes = ms.ToArray();
ms.Close();
return bytes;
}

///二进制生成图片
private Image convertImg(byte[] datas)
{
MemoryStream ms = new MemoryStream(datas);
Image img = Image.FromStream(ms,true);
ms.Close();
return img;
}