如何比较 GetPixel 方法的颜色和 Color.Black 等方法中传递的颜色?

发布于 2024-12-05 00:34:05 字数 460 浏览 2 评论 0原文

我有一个循环从图像中获取像素颜色,并尝试查看它们是否与我作为参数传递到方法中的颜色相同。

我尝试了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

百变从容 2024-12-12 00:34:05

根据 MSDN,Color.Equality 运算符...

...比较的不仅仅是颜色结构的 ARGB 值。它还对一些状态标志进行比较。如果您只想比较两个 Color 结构的 ARGB 值,请使用 ToArgb 方法比较它们

因此您使用的方法对于比较原始值是正确的

编辑

.ToArgb()< /code> 返回一个 int,因此您可以使用 == 进行比较,如果您需要,则不需要使用 .Equals()觉得太冗长了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文