Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...