C# image binary serialization

对着背影说爱祢 提交于 2020-01-17 04:30:12

问题


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


回答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



来源:https://stackoverflow.com/questions/33801746/c-sharp-image-binary-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!