Serialize a Bitmap in C#/.NET to XML

后端 未结 4 1442
悲&欢浪女
悲&欢浪女 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: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);
                }
            }
        }
    }
    

提交回复
热议问题