In Silverlight, the best way to convert between System.Drawing.Color and System.Windows.Media.Color?

I am trying to convert from System.Drawing.Color to Silverlight System.Windows.Media.Color. I am really trying to convey this color through the service.

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

I can convert argb value to 32bit int

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

      

But I don't see an appropriate method in the System.Windows.Media.Color method to convert it.

What's the best way to do this?

+1


source to share


3 answers


The 32-bit representation of System.Drawing.Color assumes a set of bytes (eg ARGB) and that each channel is 8-bit.

Silverlight doesn't 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 System.Drawing.Color, you must use the FromArgb / FromRgb methods and pass four separate components.

If necessary, you can AND-get these components from the components from the combined 32-bit value. You know (or can find out) the byte order of this color value, a knowledge that Silverlight does not have.

+1


source


Thank. This is pretty much what I did, although I had to add a .ToHtml method to output it in hexadecimal notation:

[DataMember]
public string MyColor { 
    get { 
        return ColorTranslator.ToHtml(  
                   Color.FromArgb(  
                       Color.Red.A, 
                       Color.Red.R, 
                       Color.Red.G, 
                       Color.Red.B  
            )); 
     } 
     private set{}   
}  

      



So, on the other hand, I could use the code borrowed from here http://blogs.microsoft.co.il/blogs/alex_golesh/archive/2008/04/29/colorconverter-in-silverlight-2.aspx to convert six into a solid brush.

It works, but it is ugly and seems quite complicated for what I assumed, mistakenly, would obviously be the only method. Perhaps in the next release.

+1


source


This is what Microsoft says to do - but I would not recommend it.

 String xamlString = "<Canvas xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Background=\"MistyRose\"/>";
  Canvas c = (Canvas) System.Windows.Markup.XamlReader.Load(xamlString);
  SolidColorBrush mistyRoseBrush = (SolidColorBrush) c.Background;
  Color mistyRose = mistyRoseBrush.Color;

      

Completely insane if you ask me, but this is from the MS docs!

+1


source







All Articles