0

I am struggling to binary serialize a class with image inside but I'm getting an exception:

"Type 'System.Windows.Controls.Image' in Assembly 'PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."

this is my serializer

public static bool FileSerializer<T>(string filePath, T objectToWrite,out string strError, bool append = false)
{
    using (Stream fileStream = File.Open(filePath, append ? FileMode.Append : FileMode.Create))
    {
        strError = String.Empty;
            try
            {
                var binaryFormatter = new BinaryFormatter();
                binaryFormatter.Serialize(fileStream, objectToWrite);
                return true;
            }
            catch (Exception exc)
            {
                strError = "Binary FileSerializer exception:" + exc;
                return false;
            }
        }
    }

and here is my class

    [Serializable]
    public class PcDmisData
    {
            [Serializable]
            public class Group
            {
                    public string Description;
                    public MyImage myImage;                 //optional      
                    public string Notes;                    //optional
                    public List<PartProgram> partProgramList;
            }


            [Serializable]
            public class MyImage
            {
                    public object Image;<----- this is causing the exception
                    public bool IsImageEmbedded;
            }


            [Serializable]
            public class MySoundFile
            {
                    public object SoundFile;
                    public bool IsSoundEmbedded;
            }
    ....

thanks for any help Patrick

Patrick
  • 3,073
  • 2
  • 22
  • 60
  • 2
    Well, you can't. See http://stackoverflow.com/questions/13166105/is-it-possible-to-do-net-binary-serialization-of-an-object-when-you-dont-have. http://stackoverflow.com/questions/15916661/binaryformatter-serialize-image-externalexception-a-generic-error-occurre – CodeCaster Nov 19 '15 at 10:55
  • @CodeCaster Have I got to do all this just to serialize a class with an image?!?! – Patrick Nov 19 '15 at 10:58
  • You can also save it as a base64 string and then later return it to byte data/image – Chino Nov 19 '15 at 11:32
  • @Chino, thanx that works, you might want to post that as an answer – Patrick Nov 25 '15 at 13:37

1 Answers1

1

As you suggested in the comments I made an answer out of my comment:

You can also save it as a base64 string and then later return it to byte data/image

Chino
  • 821
  • 6
  • 13