簡體   English   中英

C#圖像二進制序列化

[英]C# image binary serialization

我正在努力對內部帶有圖像的類進行二進制序列化,但出現異常:

“程序集'PresentationFramework,版本= 4.0.0.0,區域性=中性,PublicKeyToken = 31bf3856ad364e35'中的類型'System.Windows.Controls.Image'未標記為可序列化。”

這是我的序列化器

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;
            }
        }
    }

這是我的課

    [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;
            }
    ....

謝謝你的幫助Patrick

正如您在評論中建議的那樣,我從評論中做出了回答:

您也可以將其另存為base64字符串,然后將其返回到字節數據/圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM