图片与byte[]转换
希望对新学习C#的同学有帮助,代码如下!
图片 转为 byte[]
private string ImgToBase64String(Image img)
{
try
{
BinaryFormatter binFormatter = new BinaryFormatter();
MemoryStream memStream = new MemoryStream();
binFormatter.Serialize(memStream, img);
byte[] bytes = memStream.GetBuffer();
return Convert.ToBase64String(bytes);
}
catch (Exception ex)
{
return "";
}
}
byte[] 转为 图片
private Image BackGetImage(string str)
{
Image img;
if (!string.IsNullOrEmpty(str))
{
string base64 = str;
byte[] bytes = Convert.FromBase64String(base64);
MemoryStream memStream = new MemoryStream(bytes);
BinaryFormatter binFormatter = new BinaryFormatter();
img = (Image)binFormatter.Deserialize(memStream);
return img;
}
return null;
}