将图片序列化和反序列化

将如偏序列化方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//引入三个命名空间
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace StudentManager
{
    public class SerializeObjectToString
    {
        //将Object类型对象(注:必须是可序列化的对象)转换为二进制序列字符串
        public string SerializeObject(object obj)
        {
            IFormatter formatter = new BinaryFormatter();
            string result = string.Empty;
            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, obj);
                byte[] byt = new byte[stream.Length];
                byt = stream.ToArray();
                //result = Encoding.UTF8.GetString(byt, 0, byt.Length);
                result = Convert.ToBase64String(byt);
                stream.Flush();
            }
            return result;
        }

 

 反序列化方法
  //将二进制序列字符串转换为Object类型对象
        public object DeserializeObject(string str)
        {
            IFormatter formatter = new BinaryFormatter();
            //byte[] byt = Encoding.UTF8.GetBytes(str);
            byte[] byt = Convert.FromBase64String(str);
            object obj = null;
            using (Stream stream = new MemoryStream(byt, 0, byt.Length))
            {
                obj = formatter.Deserialize(stream);
            }
            return obj;
        }
    }
}

 

posted @ 2018-03-28 15:18  WhiteSpace  阅读(2679)  评论(0编辑  收藏  举报