序列化Image到byte[]

using System.Runtime.Serialization.Formatters.Binary; //引入供序列化Image对象使用

/// <summary>
/// 将byte[]转换为Image
/// </summary>
/// <param name="bytes">字节数组</param>
/// <returns>Image</returns>
public Image ReadImage(byte[] bytes)
{
     MemoryStream ms=new MemoryStream(bytes,0,bytes.Length);
     BinaryFormatter bf = new BinaryFormatter();
     object obj=bf.Deserialize(ms);   
  ms.Close(); 
  return (Image)obj;
}
/// <summary>
/// 将Image转换为byte[]
/// </summary>
/// <param name="image">Image</param>
/// <returns>byte[]</returns>
public byte[] ConvertImage(Image image)
{
     MemoryStream ms=new MemoryStream();
     BinaryFormatter bf = new BinaryFormatter();
     bf.Serialize(ms,(object)image); 
     ms.Close();
     return ms.ToArray();
}

上一篇:微信微信JS-SDK 6.0.2 填坑笔记


下一篇:iOS--九宫格奥秘(UIView)(arc4random)