0

System.Windows.Media.PixelFormatオブジェクトをシリアル化し、逆シリアル化して再作成したいと思います。私がやっていること:

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();

その後

PixelFormat pixelFormat;
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("test", FileMode.Open, FileAccess.Read, FileShare.Read);
pixelFormat = (PixelFormat)formatter.Deserialize(stream);
stream.Close();

シリアライゼーションではエラーは発生しません。このオブジェクトを逆シリアル化しようとしても、エラーは発生しませんが、返されたオブジェクトは、たとえば、BitsPerPixelフィールド内で適切ではありませんBitsPerPixel = 'pixelFormat.BitsPerPixel' threw an exception of type 'System.NotSupportedException'

@edit この問題の回避策があります。PixelFormatConverter を使用して PixelFormat オブジェクトを文字列に変換し、文字列をシリアル化する必要があります。デシリアライズ時に文字列を取得し、PixelFormatConverter を使用してそれを PixelFormat に変換します。

4

1 に答える 1