问题
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