Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize a Bitmap in C#/.NET to XML

Tags:

I want to XML-Serialize a complex type (class), that has a property of type System.Drawing.Bitmap among others.

    /// <summary>     /// Gets or sets the large icon, a 32x32 pixel image representing this face.     /// </summary>     /// <value>The large icon.</value>     public Bitmap LargeIcon { get; set; } 

I now have found out that serializing the Bitmap with the default XML serializer does not work, because it does not have a public parameterless constructor, which is mandatory with the default xml serializer.

I am aware of the following:

  • There exists a workaround, posted here: http://www.dotnetspider.com/resources/4759-XML-Serialization-C-Part-II-Images.aspx . However since this includes adding another property this seems to me a bit of a hack.
  • There is also a deep XML serializing project on sourceforge.

I rather would not like referencing another project nor extensively tweak my class to just allow xml serialization of those bitmaps.

Is there no way to keep that simple?

Many thanks, Marcel

like image 767
Marcel Avatar asked Dec 15 '09 12:12

Marcel


2 Answers

I would do something like:

[XmlIgnore] public Bitmap LargeIcon { get; set; }  [Browsable(false),EditorBrowsable(EditorBrowsableState.Never)] [XmlElement("LargeIcon")] public byte[] LargeIconSerialized {     get { // serialize         if (LargeIcon == null) return null;         using (MemoryStream ms = new MemoryStream()) {             LargeIcon.Save(ms, ImageFormat.Bmp);             return ms.ToArray();         }     }     set { // deserialize         if (value == null) {             LargeIcon = null;         } else {             using (MemoryStream ms = new MemoryStream(value)) {                 LargeIcon = new Bitmap(ms);             }         }     } } 
like image 106
Marc Gravell Avatar answered Oct 25 '22 22:10

Marc Gravell


You can also to implement ISerializable and to use SerializationInfo to deal manually with your bitmap content.

EDIT: João is right: Correct way to deal with XML serialization is to implement IXmlSerializable, not ISerializable:

public class MyImage : IXmlSerializable {     public string Name  { get; set; }     public Bitmap Image { get; set; }      public System.Xml.Schema.XmlSchema GetSchema()     {         throw new NotImplementedException();     }      public void ReadXml(System.Xml.XmlReader reader)     {         throw new NotImplementedException();     }      public void WriteXml(System.Xml.XmlWriter writer)     {         writer.WriteStartElement("Name");         writer.WriteString(this.Name);         writer.WriteEndElement();          using(MemoryStream ms = new MemoryStream())         {             this.Image.Save(ms, ImageFormat.Bmp );             byte[] bitmapData = ms.ToArray();             writer.WriteStartElement("Image");             writer.WriteBase64(bitmapData, 0, bitmapData.Length);             writer.WriteEndElement();         }     } } 
like image 24
Rubens Farias Avatar answered Oct 25 '22 22:10

Rubens Farias