图像文件序列化到XML文件中

       今天完成了图像文件序列化到XML文件中的代码(还没有经过测试)。具体的实现方式是:要进行XML序列化的类ToyIconBases实现接口IXmlSerializable ,然后调用 XML序列化类进行序列化,ToyIconBase类的代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;


namespace MonkeyToy
{
    public class ToyIconBase:IXmlSerializable
    {
        public ToyIconBase()
        {
            m_Icon= new System.Drawing.Bitmap(ToyResource.ButtonIcon);
        }

        protected Bitmap m_Icon;

        /// <summary>
        /// 考虑到序列化到XML文件中,需要将bitmap中的数据进行base64编码和界面
        /// </summary>
        //public String ToyIconBase64
        //{
        //    get
        //    {
        //       yte);

        //        return m_Icon;
        //    }
        //    set
        //    {
        //        m_Icon = value;
        //    }
        //}

        public void SetIcon(Image image)
        {
            m_Icon = new Bitmap(image);
        }
        public void SetIcon(String sImgFile)
        {
            m_Icon = new Bitmap(sImgFile);
        }

        #region IXmlSerializable 成员

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            String sIconFile = Application.ExecutablePath + ".ToyIcon.jpg";
            m_Icon.Save(sIconFile);

            int readByte = 0; //
            int bytesToRead = 100; //数据缓冲区大小
            // 打开图片文件,利用该图片构造一个文件流
            FileStream fs = new FileStream(sIconFile, FileMode.Create);
            // 使用文件流构造一个二进制读取器将基元数据读作二进制值
            BinaryWriter wr = new BinaryWriter(fs);
            //书写声明

           
            reader.ReadStartElement("ToyIcon");

            byte[] base64buffer = new byte[bytesToRead]; //开辟缓冲区
           
           
            do
            {
                readByte = reader.ReadElementContentAsBase64(base64buffer, 0, bytesToRead);
                wr.Write(base64buffer);
              

            } while ( readByte>0);


            reader.ReadEndElement();

            fs.Close();
            //加载图标文件
            m_Icon = new Bitmap(sIconFile);

            //删除图标文件
            File.Delete(sIconFile);
        }

        public void WriteXml(XmlWriter writer)
        {
            String sIconFile =Application.ExecutablePath+ ".ToyIcon.jpg";
            m_Icon.Save(sIconFile);

            int readByte = 0; //
            int bytesToRead = 100; //数据缓冲区大小
            // 打开图片文件,利用该图片构造一个文件流
            FileStream fs = new FileStream(sIconFile, FileMode.Open);
            // 使用文件流构造一个二进制读取器将基元数据读作二进制值
            BinaryReader br = new BinaryReader(fs);
            //书写声明
           
            writer.WriteStartElement("ToyIcon"); //定义节点
    

            byte[] base64buffer = new byte[bytesToRead]; //开辟缓冲区
            do
            {
                readByte = br.Read(base64buffer, 0, bytesToRead); //将数据读入字节数组
                writer.WriteBase64(base64buffer, 0, readByte); //将数组中二进制值编码为Base64并写出到XML文件

            } while (bytesToRead <= readByte);


            writer.WriteEndElement();
        
            fs.Close();
            //删除图标文件
            File.Delete(sIconFile);
        }

        #endregion

       
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值