Question Locate color within a rectangle using a Graphics object.

Thonord

Member
Joined
Oct 25, 2012
Messages
23
Programming Experience
10+
Hi, I'm an old retired Clipperhead, now a hobby VB programmer (starting from VB3).
I am managing til conversion to .NET pretty well on my own, but I find myself often doing things "the old way" instead of using the vast resources in .NET.
As an example, I would scan an area of a bitmap pixel by pixel to create a create a MemoryBitmap (using Get and SetPixel) - but no longer :) I now copy a rectangle and presto - less code and greater speed.

This leads to my questions:

1. Can I locate an unique color within a rectangle? Sort of like a function: "IsColor.BlackFound" and where?

2. Doing it the hard way, I came across something surprising.

I would get a pixel "FoundPixel as color" and compare it with color.Black
The strange thing is that even if FoundPixel contains a Black pixel, If FoundPixel = Color.Black Then - is not True (and thus No Then, the way I want Then to be).
I have found that the gotten pixel, with GetPixel, looks like this: FoundPixel.color{name=ff000, ARGB=(255,0,0,0)}
whilst the "OutOfTheBox" color .Black, kindly provided, looks like this: Color.Black{name=Black, ARGB=(255,0,0,0)}. No big deal, one might say, but a Boolean comparison begs to differ.
The solution is simple, I just declare myColor.Black and all is well. But:

Is this a "Bug"? Have I really found a bug in a MS product all by my self?

Anyhoo, Should one choose to answer only one of my questions - please make it number 1.

Number 2 is more of way to allow me a self appreciating "Bazzingaaaa!"
icon6.png
 
1. No, you have to loop through each pixel, this is fastest done using the "LockBits" method (web search if this is new to you).
2. Color compares not only the argb values, but also checks KnownColor state. One of those colors is a known color (the color named Black), the other has just a argb value. You can call ToArgb on both colors and compare those values only.
 
Thank you JohnH.
I have googled and found:)
Now all I need to find out is how to change the status of my Question to Ansewred.
 
Back
Top