I would like to serialize System.Windows.Media.PixelFormat
object, and then recreate it by deserialization. What I'm doing:
BitmapSource bitmapSource = backgroundImage.ImageSource as BitmapSource;
PixelFormat pixelFormat = bitmapSource.Format;
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("test", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, pixelFormat);
stream.Close();
and then
PixelFormat pixelFormat;
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("test", FileMode.Open, FileAccess.Read, FileShare.Read);
pixelFormat = (PixelFormat)formatter.Deserialize(stream);
stream.Close();
The serialization doesn't give any error. When I try to deserialize this object it also doesn't give any error, but the returned object isn't good, for example in the BitsPerPixel
field it has BitsPerPixel = 'pixelFormat.BitsPerPixel' threw an exception of type 'System.NotSupportedException'
@edit I have a work around of this problem. We must use PixelFormatConverter to convert PixelFormat object to string and then serialize the string. When deserializing we get the string and using PixelFormatConverter convert it back to PixelFormat.