Serialize a Bitmap in C#/.NET to XML

后端 未结 4 1402
悲&欢浪女
悲&欢浪女 2020-11-27 06:12

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

    /// <         


        
相关标签:
4条回答
  • 2020-11-27 06:25

    The BitMap class has not been designed to be easily XML Serialized. So, no, there's not simple way to correct a design decision.

    0 讨论(0)
  • 2020-11-27 06:26

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 06:27

    Implement IXmlSerializable and then handle all the serialization details yourself.

    Since you say it's a large type and you only have a problem with the bitmap consider doing something like this:

    public class BitmapContainer : IXmlSerializable
    {
        public BitmapContainer() { }
    
        public Bitmap Data { get; set; }
    
        public XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }
    
        public void ReadXml(XmlReader reader)
        {
            throw new NotImplementedException();
        }
    
        public void WriteXml(XmlWriter writer)
        {
            throw new NotImplementedException();
        }
    }
    
    public class TypeWithBitmap
    {
        public BitmapContainer MyImage { get; set; }
    
        public string Name { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-27 06:29

    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);
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题