简体   繁体   中英

C# image binary serialization

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM