BinaryFormatter.Serialize( Image ) - ExternalException - A generic error occurred in GDI+

后端 未结 3 1169
清酒与你
清酒与你 2021-01-14 10:35

When I try to Serialize some images using the BinaryFormatter, I\'ll get a ExternalException - A generic error occurred in GDI+.\" After scratching my head for awhi

3条回答
  •  有刺的猬
    2021-01-14 11:08

    Although the Bitmap class is marked as [Serializable], it does not actually support serialisation. The best you can do is serialise the byte[] containing the raw image data and then re-create it using a MemoryStream and the Image.FromStream() method.

    I can't explain the inconsistent behaviour you're experiencing; for me, it fails unconditionally (although I first discovered this when trying to marshal images between different app domains, rather than manually serialising them).

提交回复
热议问题