Serializing System.Drawing.Color in .NET.

I used the default .NET serialization for the class with a member of System.Drawing.Color. The code is now used by people, and I need to add an additional element to the class, but still deserialize the old versions.

So, I tried the standard way to do this: the ISerializable interface, using the SerializationInfo methods to get int and string elements.

Problem: my class also has a System.Drawing.Color member, but SerializationInfo does not provide a GetColor method to read this data type. I tried to get it as an int and as a string, and dropped it in System.Drawing.Color, but no luck.

Does anyone know how to deserialize System.Drawing.Color from SerializationInfo?

+3
source share
2 answers

Using ISerializable is not a recommended versioning method. [Optional Field], the inputs and outputs are well described in this MSDN article.

Answering your question: SerializationInfo.GetValue ("fieldName", typeof (Color)) should give you color. You will need to specify the return value.

+1
source

I have used something similar in the past.

 <Xml.Serialization.XmlIgnore()> Public BackColour As Drawing.Color

        Public Property xmlBackColour() As Integer
            Get
                Return BackColour.ToArgb
            End Get
            Set(ByVal value As Integer)
                BackColour = Drawing.Color.FromArgb(value)
            End Set
        End Property
+1
source

Source: https://habr.com/ru/post/1737744/


All Articles