Click here to Skip to main content
15,893,722 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends,

Is there any way to get the RGB value from ColorDialog in WPF?

I have tried following code but but here i'm getting error as

Error	4	Cannot implicitly convert type 'System.Drawing.Color' to 'System.Windows.Media.Color'


Code :
C#
System.Windows.Forms.ColorDialog cd = new System.Windows.Forms.ColorDialog();
 Color clr = cd.Color;



Thanks
Posted

The Color class has properties
C#
public byte R { get; }
public byte G { get; }
public byte B { get; }


This snippet shows how to get the RGB values:
C#
ColorDialog cd = new ColorDialog();

DialogResult result = cd.ShowDialog();

if (result == DialogResult.OK)
{
    Color color = cd.Color;

    byte R = color.R;
    byte G = color.G;
    byte B = color.B;
}
 
Share this answer
 
Cannot implicitly convert type 'System.Drawing.Color' to 'System.Windows.Media.Color'

The problem is that you're using the Windows Forms ColorDialog, which returns a System.Drawing.Color, whereas WPF tends to use System.Windows.Media.Color.

You could switch to using a WPF colour picker - for example, this one[^].

Alternatively, you'll need to convert the returned colour:
C#
var cd = new System.Windows.Forms.ColorDialog();
...
var winFormsColor = cd.Color;
var wpfColor = System.Windows.Media.Color.FromArgb(winFormsColor.A, winFormsColor.R, winFormsColor.G, winFormsColor.B);
 
Share this answer
 
Comments
Znaneswar K 26-Feb-15 4:29am    
I have tried this one but still I'm getting the same error :( Any other ways?

Thanks
Richard Deeming 26-Feb-15 7:08am    
You won't be getting the same error from the code I posted - there's no attempt to assign a System.Drawing.Color to a System.Windows.Media.Color.

Either you're getting a different error, or you've changed the code.
Znaneswar K 26-Feb-15 7:24am    
Sorry I confused. its working fine now :)

Thanks a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900