C# Color.GetPixel().equals(Color.Blue)的结果为false
控制台输出:C# Color.GetPixel().equals(Color.Blue)的结果为false,c#,colors,getpixel,C#,Colors,Getpixel,控制台输出: Color c1 = image.GetPixel (7, 400); Color c2 = Color.Blue; Console.WriteLine (image.GetPixel (7, 400)); Console.WriteLine (Color.Blue); Console.WriteLine (c1.Equals(c2)); 我是C#的新手,我不知道为什么会返回false。有人能告诉我为什么这不起作用吗 我试着在这种情况下使用它 Color [A=255, R=0,
Color c1 = image.GetPixel (7, 400);
Color c2 = Color.Blue;
Console.WriteLine (image.GetPixel (7, 400));
Console.WriteLine (Color.Blue);
Console.WriteLine (c1.Equals(c2));
我是C#的新手,我不知道为什么会返回false。有人能告诉我为什么这不起作用吗
我试着在这种情况下使用它
Color [A=255, R=0, G=0, B=255]
Color [Blue]
False
for(inti=0;i
正如您已经注意到的,以下示例将返回false:
for (int i = 0; i < image.Height; i++) //loop through rows
{
for (int j = 0; j < image.Width; j++) //loop through columns
{
//Console.WriteLine ("i = " + i);
//Console.WriteLine ("j = " + j);
if ((image.GetPixel (i, j)) == Color.Blue)
{
return new Tuple<int, int>(i,j);
}
if (i == image.Height-1 && j == image.Width-1)
{
Console.WriteLine ("Image provided does not have a starting point. \n" +
"Starting points should be marked by Blue.");
Environment.Exit (0);
}
}
}
原因有点难以理解:
如果查看位图类的源代码,您会发现
Bitmap bmp = new Bitmap(1, 1);
bmp.SetPixel(0, 0, Color.Blue);
Color c1 = bmp.GetPixel(0, 0);
Console.WriteLine("GetPixel:" + c1);
Console.WriteLine("Color:" + Color.Blue);
Console.WriteLine("Equal?:" + c1.Equals(Color.Blue));
Console.ReadLine();
Color.Equals()
的文档说明:
要仅根据颜色的ARGB值比较颜色,应使用
ToArgb方法。这是因为平等和平等的成员
确定等效性时使用的不仅仅是
颜色。例如,不考虑Black和FromArgb(0,0,0)
相等,因为黑色是命名颜色,而FromArgb(0,0,0)不是
因此,返回的颜色不等于Color.Blue
——即使就ARGB值而言它是Color.Blue
要绕过此问题,请使用:
public Color GetPixel(int x, int y) {
//lot of other code
return Color.FromArgb(color);
}
作为示例的最后一行。所以如果我将这两个值都转换成Argb,结果会是真的,但有没有办法不这样做呢?我想你需要做Color c1=Color.FromARGB(image.GetPixel(7400));说它不能把颜色转换成INT是的,这就是我最后要做的。非常感谢!
Console.WriteLine("Equal?:" + c1.ToArgb().Equals(Color.Blue.ToArgb()));