如何来比较与getPixel方法,并在像Color.Black的方法通过一个颜色的颜色? [英] How to compare a Color by the GetPixel Method and a Color passed in a method like Color.Black?

查看:326
本文介绍了如何来比较与getPixel方法,并在像Color.Black的方法通过一个颜色的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,从图像获取pixelcolors和尝试,看看他们是一样的,因为我传递到方法参数的颜色。



我试过在等于方法,但它不工作。我也试过 ToKnown 方法。
它看起来像那场比赛并不怎么一回事,因为该合成两种颜色不匹配的值工作



例如:



使用GetPixel:

  {名称= FF000000,ARGB =(255,0,0,0 )} 

Col​​or.Black:

  {名称=黑色,ARGB =(255,0,0,0)} 
如果(pixelColor.ToArgb()。等于(startingOffsetColor.ToArgb())){}

以上工作的代码,但我还是想知道是否有更好的方法或可以在任何方法。减少任何CPU开销,因为我用这一个循环语句里


解决方案

According~~V到MSDN 的Color.Equality操作...




...比较比色结构的ARGB值的更多。它也做了一些状态标志的比较。如果你想比较只有两个颜色结构ARGB值,

所以该方法所使用使用ToArgb法


进行比较是比较原始值



修改



.ToArgb()返回 INT ,所以你可以只使用 == 进行比较,你并不需要使用 .Equals()如果你觉得它太冗长。


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.

解决方案

According to MSDN, the Color.Equality Operator...

...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

EDIT

.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.Black的方法通过一个颜色的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆