Compare RBG values and get color at mouse click location

Blake81

Well-known member
Joined
Feb 23, 2006
Messages
304
Location
Georgia, USA
Programming Experience
1-3
I want to make a program for someone at work that does the following:

-Lets them select an image to load into a PictureBox
-Gets the RGB value of the color where they click on the image
-Compares that RGB value to a set of colors which I will store in a file or hash table and picks the closest match.

This seems like a pretty simple project, but I don't know how to do it. I don't know about getting the RGB value at the cursor location, and I'm not sure how to go about doing the comparison to pick the closest color. I would appreciate any help.
 
VB.NET:
Dim thecolor As Color = DirectCast(PictureBox1.Image, Bitmap).GetPixel(x, y)
x/y is image coordinates, zero based for image width/height in pixel dimensions. Use the MouseDown event on Picturebox, it supplies coordinates in e parameter.
 
GetPixel

I would like to know all of the uses of getpixel. Is there a link you can send, or a site, or a book.

Thanks
 
There is only one use of GetPixel function and that is to retrieve the Color value at specified coordinate.
 
Nice...

DirectCast is kinda like WildCard... just does what you need when all the painful methods I spent six hours trying and searching for fail.... however, this one fails too if I try to use it on a Graphics object. I actually figured out how to get around it by keeping two copies of the PictureBox on hand: one for graphics (and therefore display), and one for Image (for accessing pixels). But, if someone is ever around and knows how to access arbitrary pixels in a graphics object (either that or how to make an image display in a PictureBox), feel free to post it here. Thanks for the DirectCast: I hope it will come into use some time again.
 
Back
Top