How to test whether a System.Drawing.Color
and a System.Windows.Media.Color
describe the same colour?
I tried
colour1 == colour2
but I get an error
Operator '==' cannot be applied to operands of type 'System.Drawing.Color' and 'System.Windows.Media.Color
You have 2 options:
Convert from one type to the other, which is covered here , and then use the '==' operator.
Compare the individual components. Since both of them have the R, G, B, A properties as bytes, you can simply do:
bool ColorsEqual (System.Drawing.Color c1, System.Windows.Media.Color c2) { return c1.R == c2.R && c1.G == c2.G && c1.B == c2.B && c1.A == c2.A; }
As there is no operator==
overloaded for these two types, you could acquire the string-values of the color or the ARGB-values.
System.Drawing.Color c1 = System.Drawing.Color.FromArgb(255,0,0,0);
System.Windows.Media.Color c2 = System.Windows.Media.Color(255,0,0,0);
if(c1.A == c2.A && c1.R == c2.R && ...
You can make an extension method to the System.Drawing.Color that converts to a System.Windows.Media.Color and then compare on the System.Windows.Media.Color type:
public static System.Windows.Media.Color ToMediaColor(this System.Drawing.Color color)
{
return System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.