C#图像二进制序列 [英] C# image binary serialization

查看:143
本文介绍了C#图像二进制序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我奋力二进制序列化与内部图像的类,但我发现了异常:

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

类型System.Windows.Controls.Image'在大会'presentationFramework,版本= 4.0.0.0,文化=中性公钥= 31bf3856ad364e35'未标记为可序列化。

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

这是我的串行

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

感谢您的帮助
帕特里克

thanks for any help Patrick

推荐答案

当您在留言建议我做了一个答案我的评论:

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

您也可以将其保存为Base64字符串,然后稍后返回它字节数据/图片

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

这篇关于C#图像二进制序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆