R – In Silverlight, what is the best way to convert between a System.Drawing.Color and a System.Windows.Media.Color


I'm trying to convert from a System.Drawing.Color to a Silverlight System.Windows.Media.Color. I'm actually trying to pass this color through a service.

The System.Drawing.Color, passed over the wire, does not serialize the argb value independently.

I can convert the argb value to a 32-bit int

public int MyColor { get { return Color.Red.ToArgb(); } set {} }  

But I don't see any corresponding method in System.Windows.Media.Color to convert that back.

What is the best way to do this?

Best Solution

The 32-bit representation of System.Drawing.Color assumes a set byte-ordering (e.g ARGB) and that each channel is represented by 8-bits.

Silverlight does not make these assumptions. Instead Media.Color is stored as four 32-bit floating point values and the ordering is based on the color profile.

To create a Media.Color value from a System.Drawing.Color you should use the FromArgb / FromRgb methods and pass in the four separate components.

If necessary You could get these components by AND'ing out the components from the combined 32-bit value. You know (or can find out) the order of the bytes in this color value, which is knowledge that Silverlight does not have.

Related Question