0

I'm making a small application to get more familiar with serialisation. I'm currently trying to serialize an image using xml. So far I've just created a property that will do the serialization. However I'm not sure where to go from here and what I should do next.

I want to be able to tell it what image to serialize and run it from my Main method in my console application

My code is as follows

    [XmlIgnore]
    public static 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);
                }
            }
        }
    }
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • 2
    I wouldn't recommend putting binary data such as images inside xml. – Magnus Nov 23 '17 at 15:28
  • For specific requirement reasons I've had to store binary data as **base64** encoded values in XML and it increases the size of the data for storage by approximarely 37% so I wouldn't suggest that in case you considered it - https://stackoverflow.com/questions/11402329/base64-encoded-image-size – Matt Hogan-Jones Nov 23 '17 at 15:30

0 Answers0