8 Replies - 2596 Views - Last Post: 06 February 2012 - 09:11 AM Rate Topic: -----

#1 RKrogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 08:56 AM

Hi!
I am doing a small project for some Health Care Students. It's a small program randomizing sentences.
I have a class SentenceManager containing a List<Sentence> (strings basically) and a List<Image>.
Serializing the List<Sentence> is no problem but when I try to serialize List<Image> the program crashes.

Here is a code snippet of my SentenceManager-class:
 [Serializable]
    [XmlRoot("sentenceManager")]
    public class SentenceManager
    {
        /// <summary>
        /// FIELDS
        /// </summary>
        private List<Sentence> senList;
        private List<Image> imgList;

        /// <summary>
        /// CONSTRUCTOR
        /// </summary>
        public SentenceManager()
        {
            senList = new List<Sentence>();
            imgList = new List<Image>();
        }

        /// <summary>
        /// PROPERTIES
        /// </summary>
        [XmlElement("SentenceList")]
        public List<Sentence> sentenceList
        {
            get { return senList; }
            set { senList = value; }
        }

        [XmlElement("ImageList")]
        public List<Image> imageList
        {
            get { return imgList; }
            set { imgList = value; }
        }  

        /// <summary>
        /// METHODS
        /// 
        /// Add sentence to the list of sentences.
        /// </summary>
        /// <param name="senToAdd"></param>
        public void addSentence(Sentence senToAdd)
        {
            senList.Add(senToAdd);
        }

        /// <summary>
        /// Remove a given sentence from the list of sentences.
        /// </summary>
        /// <param name="senToRemove"></param>
        public void removeSentence(Sentence senToRemove)
        {
            senList.Remove(senToRemove);
        }

        public void addImage(Image imageToAdd)
        {
            imgList.Add(imageToAdd);
        }

        public void removeImage(Image imageToRemove)
        {
            imgList.Remove(imageToRemove);
        }

        public void removeImage(int index)
        {
            imgList.RemoveAt(index);
        }
    }


This post has been edited by tlhIn`toq: 05 February 2012 - 09:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How do I serialize (xml) when I have List<Image>?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 09:10 AM

What is the error when it crashes?
I've never tried to serialize an Image. You could turn it into a byte[] then serialize those. Its a simply convert to/from byte[] to bitmap.

Your code shows the clas you are serializing but not the (de)serialization code itself.

I did a tutorial on XML serialization if it helps. FAQ 8


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 RKrogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 09:14 AM

Thanks!
I'm trying to convert the Images to strings to get this to work right now. Don't know if it's supposed to work, though.

The error I get is that it was not expecting a Bitmap to be serialized in the stream. It advices me to use the XmlInclude for these unknown datatypes but that didn't work out either.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 09:19 AM

Don't try to convert a bitmap to a string. That's just fugly. It opens up to all sorts of errors like globalization for 1 or 2 byte character sets and so on. Don't go there.

Stick with converting to/from a byte[]

http://www.codeproje...y-to-Image-Conv

View PostRKrogh, on 05 February 2012 - 10:14 AM, said:

The error I get is that it was not expecting a Bitmap to be serialized in the stream. It advices me to use the XmlInclude for these unknown datatypes but that didn't work out either.


Can you show us the code you are using for doing the serialization and deserialization?
Was This Post Helpful? 0
  • +
  • -

#5 RKrogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 09:25 AM

Okey, I'll try to do the byte[] as well. What's the difference? Only cosmetic or has it some greater features?

This code works out fine though. I call the methods before and after serializeation.
[Serializable]
    [XmlRoot("sentenceManager")]
    public class SentenceManager
    {
        /// <summary>
        /// FIELDS
        /// </summary>
        private List<Sentence> senList;
        private List<Image> imgList;
        private List<String> imgStringList;

        /// <summary>
        /// CONSTRUCTOR
        /// </summary>
        public SentenceManager()
        {
            senList = new List<Sentence>();
            imgList = new List<Image>();
            imgStringList = new List<string>();
        }

        /// <summary>
        /// PROPERTIES
        /// </summary>
        [XmlElement("SentenceList")]
        public List<Sentence> sentenceList
        {
            get { return senList; }
            set { senList = value; }
        }

        [XmlIgnore]
        public List<Image> imageList
        {
            get { return imgList; }
            set { imgList = value; }
        }

        [XmlElement("ImageList")]
        public List<String> imageStringList
        {
            get { return imgStringList; }
            set { imgStringList = value; }
        }

        /// <summary>
        /// METHODS
        /// 
        /// Add sentence to the list of sentences.
        /// </summary>
        /// <param name="senToAdd"></param>
        public void addSentence(Sentence senToAdd)
        {
            senList.Add(senToAdd);
        }

        public void setImageStringList()
        {
            foreach (Image img in imgList)
            {
                MemoryStream ms = new MemoryStream();
                img.Save(ms, img.RawFormat);
                byte[] array = ms.ToArray();
                imgStringList.Add(Convert.ToBase64String(array));
            }
        }

        public void setImageListFromString()
        {
            if (imgStringList.Count > 0)
            {
                foreach (string s in imgStringList)
                {
                    byte[] array = Convert.FromBase64String(s);
                    Image image = Image.FromStream(new MemoryStream(array));
                    imageList.Add(image);
                }
            }
        }

        /// <summary>
        /// Remove a given sentence from the list of sentences.
        /// </summary>
        /// <param name="senToRemove"></param>
        public void removeSentence(Sentence senToRemove)
        {
            senList.Remove(senToRemove);
        }

        public void addImage(Image imageToAdd)
        {
            imgList.Add(imageToAdd);
        }

        public void removeImage(Image imageToRemove)
        {
            imgList.Remove(imageToRemove);
        }

        public void removeImage(int index)
        {
            imgList.RemoveAt(index);
        }
    }


Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 10:04 AM

A byte is a type... Image an abstract class. As we all know, GUI controls (Buttons, Forms etc) can't be serialized, and although I don't *know* it, I suspect you're bumping up the same limitation for the same reason.

Byte is a much simplier entity to deal with. Since you're code is working with strings, which are really just char[], it makes sense it should work with a byte[]
Was This Post Helpful? 0
  • +
  • -

#7 RKrogh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 26-April 11

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 11:19 AM

Thank you for all your help!
I re-did the string part into byte[], It practically looks the same but I guess the layout is a bit simpler and easy enough to understand.
I guess you're right. Trying to find the exact problem with direct serializing an Image, the InnerException told me Null. The problem was triggered by the fact that Image, as you describe it, is a dynamic instance of an object. It could've been any type of Image but it wanted it specified, hence the confusion when it tried to serialize the dynamic object. I think that one could try to be specific in ones code when it comes to serializing a bitmap or the like. But as my user can add any given image-type this was the better solution.

Thank you again for your help!
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: How do I serialize (xml) when I have List<Image>?

Posted 05 February 2012 - 11:49 AM

Glad I could help
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4485
  • View blog
  • Posts: 7,813
  • Joined: 08-June 10

Re: How do I serialize (xml) when I have List<Image>?

Posted 06 February 2012 - 09:11 AM

In case you're still curious, byte[] is a binary representation of something. For future reference, if you ever do need to represent something binary as a string (for instance, transmission across a web service), you can use what's known as a Base64 string:

string b64 = Convert.ToBase64String(bytes);


Base64 is a convenient way to represent binary in a shorter format.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1