今天完成了图像文件序列化到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
}
}
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
}
}