/// <summary>
/// 二进制转图片
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public static Image TransformImage(string imageStr)
{
byte[] bytes = Convert.FromBase64String(imageStr);
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter bf = new BinaryFormatter();
Image img = (Image)bf.Deserialize(ms);
return img;
}
/// <summary>
/// 图片转二进制
/// </summary>
/// <param name="imagepath"></param>
/// <returns></returns>
public static string TransformByte(Image img)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, img);
byte[] bytes = ms.GetBuffer();
string imageStr = Convert.ToBase64String(bytes);
return imageStr;
}
/// 二进制转图片
/// </summary>
/// <param name="streamByte"></param>
/// <returns></returns>
public static Image TransformImage(string imageStr)
{
byte[] bytes = Convert.FromBase64String(imageStr);
MemoryStream ms = new MemoryStream(bytes);
BinaryFormatter bf = new BinaryFormatter();
Image img = (Image)bf.Deserialize(ms);
return img;
}
/// <summary>
/// 图片转二进制
/// </summary>
/// <param name="imagepath"></param>
/// <returns></returns>
public static string TransformByte(Image img)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, img);
byte[] bytes = ms.GetBuffer();
string imageStr = Convert.ToBase64String(bytes);
return imageStr;
}
本文介绍了一种将图片转换为二进制字符串及从二进制字符串还原图片的方法。通过使用.NET Framework中的BinaryFormatter类和MemoryStream,实现了图片文件的序列化与反序列化过程。

4215

被折叠的 条评论
为什么被折叠?



