如何比较 GetPixel 方法的 Color 和 Color.Black 等方法中传递的 Color? [英] How to compare a Color by the GetPixel Method and a Color passed in a method like Color.Black?
问题描述
我有一个从图像中获取像素颜色的循环,并尝试查看它们是否与我作为参数传递给方法的颜色相同.
I have a loop that gets pixelcolors from an image and try to see if they are the same as the Color I passed into the method as parameter.
我尝试了 Equals
方法,但它不起作用.我还尝试了 ToKnown
方法.看起来这种匹配不起作用,因为合成两种颜色的值不匹配.
I tried the Equals
method but it doesn't work. I also tried the ToKnown
method.
It looks like that match doesn't work beacuse the values that synthesize the two colors don't match.
示例:
使用 GetPixel:
With GetPixel:
{Name=ff000000, ARGB=(255, 0, 0, 0)}
颜色.黑色:
{Name=Black, ARGB=(255, 0, 0, 0)}
if (pixelColor.ToArgb().Equals(startingOffsetColor.ToArgb())) { }
上面的代码有效,但我仍然想知道是否有更好的方法或任何可以减少任何 CPU 开销的方法,因为我在循环语句中使用它.
The code above works, but I still want to know if there is any better method or any method that can reduce any CPU overhead, because I'm using this inside a loop statement.
推荐答案
根据 MSDN,Color.Equality 运算符...
According to MSDN, the Color.Equality Operator...
...比较的不仅仅是颜色结构的 ARGB 值.它还对一些状态标志进行了比较.如果只想比较两个 Color 结构的 ARGB 值,请使用 ToArgb 方法进行比较
...compares more than the ARGB values of the Color structures. It also does a comparison of some state flags. If you want to compare just the ARGB values of two Color structures, compare them using the ToArgb method
所以您使用的方法比较原始值是正确的
So the method that you are using is correct for comparing the raw values
编辑
.ToArgb()
返回一个 int
所以你可以只使用 ==
进行比较,你不需要使用 .Equals()
如果你觉得它太冗长了.
.ToArgb()
returns an int
so you can just use ==
for comparison, you don't need to use .Equals()
if you find it too verbose.
这篇关于如何比较 GetPixel 方法的 Color 和 Color.Black 等方法中传递的 Color?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!