Reputation: 543
Why is the following conditional failing?
brush = new Bitmap(10, 10);
brush.SetPixel(1, 1, Color.Black);
if (brush.GetPixel(1, 1) == Color.Black)
{
MessageBox.Show("hello");
}
// Will not show "hello"
Upvotes: 0
Views: 149
Reputation: 1217
The == operator has following definition:
public static bool operator ==(Color left, Color right)
{
if (left.value != right.value || (int) left.state != (int) right.state || (int) left.knownColor != (int) right.knownColor)
return false;
if (left.name == right.name)
return true;
if (left.name == null || right.name == null)
return false;
else
return left.name.Equals(right.name);
}
If you look at Color.Black
and result from GetPixel
in debug mode you will see differences.
Upvotes: 0
Reputation: 9191
Your pixel is indeed black, but it seems to fail because Color.Black
is a KnownColor
and NamedColor
while ff000000
(the result of your GetPixel) is not. You can change the condition to verify directly the ARGB values instead :
if (brush.GetPixel(1, 1).ToArgb() == Color.Black.ToArgb())
{
MessageBox.Show("hello");
}
Upvotes: 3