wpf object 对象转 byte[] 数组

方法一:
前提:

Application.Current.Properties[“APicture_byte”] = byte[]对象

byte[] captureData = (byte[])Application.Current.Properties[“APicture_byte”];//byte[]存放着图片

方法二:

///
/// 将一个object对象序列化,返回一个byte[]
///
/// 能序列化的对象
///
public static byte[] ObjectToBytes(object obj)
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer();
}
}

    /// <summary> 
    /// 将一个序列化后的byte[]数组还原         
    /// </summary>
    /// <param name="Bytes"></param>         
    /// <returns></returns> 
    public static object BytesToObject(byte[] Bytes)
    {
        using (MemoryStream ms = new MemoryStream(Bytes))
        {
            IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms);
        }
    }
发布了115 篇原创文章 · 获赞 36 · 访问量 9870

猜你喜欢

转载自blog.csdn.net/weixin_44548307/article/details/103901803