XML Serialization in C#
This article helps to how to depicts a class called Picture which has a Bitmap as its field
In one of the earlier code snippets: titled “XML Serialization in C# - Part I (Textual information)", I discussed the techniques of serializing/de-serializing data to and from XML and load/ save that into a custom class which abstracts the XML information. That was about text based information. Likewise, it is also possible to do XML serialization for pictorial information in the form of bytes. This technique involves getting the bytes out of the source picture (e.g. Bitmap) and then serializes this array of bytes into the XML. And that's it! You can deserialize the XML file to reconstruct the pictorial data from the bytes. These techniques are useful when you want to combine textual and pictorial information in a single XML file.
The example given here depicts a class called Picture which has a Bitmap as its field. The XML file would contain a single element(i.e. root) called picture and has an attribute called “source"; which value is the bytes extracted from the source Bitmap. Note that I have reused the class XMLObjectSerializer submitted earlier in the Part I mentioned above.
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Xml.Serialization;
//root name is called "picture"
[XmlRoot(ElementName="picture")]
public class Picture
{
Bitmap source;
// Set serialization to IGNORE this property
// because the 'PictureByteArray' property
// is used instead to serialize
// the 'Picture' Bitmap as an array of bytes.
[XmlIgnore]
public Bitmap Source
{
get { return source; }
set { source = value; }
}
// Set PictureByteArray Property
// to be an attribute of the Picture node
[XmlAttribute("source")]
public byte[] PictureByteArray
{
get
{
//get a TypeConverter object for converting Bitmap to bytes
TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));
return (byte[])converter.ConvertTo(source, typeof(byte[]));
}
set
{
source = new Bitmap(new MemoryStream(value));
}
}
}
Here is a code sample for a class I reused from my previous posting for serialization:
using System;
using System.IO;
using System.Xml.Serialization;
public class XMLObjectSerializer
{
public static void Save(T obj, string flieName)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
//Create a FileStream object connected to the target file
FileStream fileStream = new FileStream(flieName, FileMode.Create);
serializer.Serialize(fileStream, obj);
fileStream.Close();
}
public static T Load(string fileName)
{
XmlSerializer deserializer = new XmlSerializer(typeof(T));
T obj = (T)deserializer.Deserialize(new FileStream(fileName, FileMode.Open));
return obj;
}
}
Here is some test code:
Picture picture = new Picture();
picture.Source = new Bitmap(@"D:\photo_student_IS0702.bmp");
XMLObjectSerializer
Picture picture2 = XMLObjectSerializer
byte[] bytes = picture2.PictureByteArray;
FileStream stream = File.OpenWrite(@"D:\copy_photo_student_IS0702.bmp");
stream.Write(bytes, 0, bytes.Length);
stream.Close();