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);
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章