如何比较 GetPixel 方法的颜色和 Color.Black 等方法中传递的颜色?
我有一个循环从图像中获取像素颜色,并尝试查看它们是否与我作为参数传递到方法中的颜色相同。
我尝试了 Equals
方法,但它不起作用。我还尝试了 ToKnown
方法。 看起来该匹配不起作用,因为合成两种颜色的值不匹配。
示例:
使用 GetPixel:
{Name=ff000000, ARGB=(255, 0, 0, 0)}
Color.Black:
{Name=Black, ARGB=(255, 0, 0, 0)}
if (pixelColor.ToArgb().Equals(startingOffsetColor.ToArgb())) { }
上面的代码可以工作,但我仍然想知道是否有更好的方法或任何可以减少 CPU 开销的方法,因为我在循环语句中使用它。
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.
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.
Example:
With GetPixel:
{Name=ff000000, ARGB=(255, 0, 0, 0)}
Color.Black:
{Name=Black, ARGB=(255, 0, 0, 0)}
if (pixelColor.ToArgb().Equals(startingOffsetColor.ToArgb())) { }
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MSDN,Color.Equality 运算符...
因此您使用的方法对于比较原始值是正确的
编辑
.ToArgb()< /code> 返回一个
int
,因此您可以使用==
进行比较,如果您需要,则不需要使用.Equals()
觉得太冗长了。According to MSDN, the Color.Equality Operator...
So the method that you are using is correct for comparing the raw values
EDIT
.ToArgb()
returns anint
so you can just use==
for comparison, you don't need to use.Equals()
if you find it too verbose.