5 Replies - 4929 Views - Last Post: 17 February 2009 - 12:27 AM Rate Topic: -----

#1 Gypsy09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Need help with getting color of Pixel

Post icon  Posted 15 February 2009 - 11:51 PM

Could someone please show me how to make this work. I have a form with a picbox and when I move my mouse over it I want to capture the color of the pixel in the picture and change the backcolor of a Labels to the same color.

Here is what I have so far:

private: System::Void pictureBox1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {

int x=System::Windows::Forms::Cursor::Position.X;
int y=System::Windows::Forms::Cursor::Position.Y;
String^ xp = Convert::ToString(x);
String^ yp = Convert::ToString(y);
this->lblOne->Text = xp;
this->lblTwo->Text = yp;

//This is where I am having problems
HDC hDC = GetDC(NULL);
COLORREF pixelColor = GetPixel(hDC,x,y);
ReleaseDC(NULL,hDC);

//I also need to know how to convert COLORREF to System::Drawing::Color for this to work
this->lblOne->BackColor = pixelColor;
this->lblTwo->BackColor = pixelColor;

}



Thanks

This post has been edited by Gypsy09: 16 February 2009 - 12:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with getting color of Pixel

#2 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: Need help with getting color of Pixel

Posted 16 February 2009 - 12:34 AM

Well if we pull the image out of the picturebox, construct a bitmap with it, then we can extract the color of a pixel at coordinates and do what we like with the color.

int x=System::Windows::Forms::Cursor::Position.X;
int y=System::Windows::Forms::Cursor::Position.Y;
String^ xp = Convert::ToString(x);
String^ yp = Convert::ToString(y);
this->lblOne->Text = xp;
this->lblTwo->Text = yp;

//Pull the image out of the picturebox
Image^ i = pictureBox1->Image;

// Build a new bitmap out of the image
Bitmap^ b = gcnew Bitmap(i);

// Get the pixel color at coordinates x, y
Color c = b->GetPixel(x,y);

// Here we simply show the color structure, but you can pull the RGB out of it as needed
MessageBox::Show(c.ToString());




Here in the example above we pull the image out of our picturebox, build a bitmap and get the pixel at coordinate x,y. We then show the color structure in a messagebox, but you could easily do whatever you like with this new color object.

Enjoy!

"At DIC we be getpixel using code ninjas... no matter which pixel you pull out of us, it will always be RGB 000000 because we are that dark and sinister!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Gypsy09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Need help with getting color of Pixel

Posted 16 February 2009 - 01:49 AM

Hey thanks for the help you are getting me closer but it only works some of the time. It throws this at me:

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Drawing.dll
Additional information: Parameter must be positive and < Height.

Any Ideas? I think it has something to do with it is taking the picture out and starting it at 0,0. So the picture is 600x400 if I goto 599,399 and click it works but if I goto 601,401 it throws the exception.

I am using the following image for testing:
Posted Image

Here is the Code:
	private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
int x=System::Windows::Forms::Cursor::Position.X;
int y=System::Windows::Forms::Cursor::Position.Y;
String^ xp = Convert::ToString(x);
String^ yp = Convert::ToString(y);

//Pull the image out of the picturebox
Image^ i = pictureBox1->Image;
// Build a new bitmap out of the image
Bitmap^ b = gcnew Bitmap(i);
// Get the pixel color at coordinates x, y
Color c = b->GetPixel(x,y);
// Here we simply show the color structure, but you can pull the RGB out of it as needed
MessageBox::Show(c.ToString());
			 }

	private: System::Void pictureBox1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
//Get Mouse Position
int x=System::Windows::Forms::Cursor::Position.X;
int y=System::Windows::Forms::Cursor::Position.Y;

//Convert int to String
String^ xp = Convert::ToString(x);
String^ yp = Convert::ToString(y);

//Display Mouse Position in Labels
this->lblOne->Text = xp;
this->lblTwo->Text = yp;
			 }



And insted of using the MsgBox can you show me how to convert it so I can change the backcolor of a Label?

Thanks

This post has been edited by Gypsy09: 16 February 2009 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 Gypsy09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Need help with getting color of Pixel

Posted 16 February 2009 - 11:06 PM

OK I am getting closer. I have it all working except one glitch. I need a different method for pulling out X and Y from the System::String::xpnt and System::String::ypnt I created.

The String looks like this:
{X=100,Y=200}
but when I use the Substring it only works when X and Y are both 3 digits.

This will also help me parse out the RGB values that has a sting that looks like this:
Color [A=255, R=255, G=255, B=255]
or
Color [A=1, R=10, G=255, B=0]
or
Color [A=10, R=255, G=0, B=10]
and so on

If anyone knows a built in method I could use to parse my Strings I would greatly appreciate it.

	private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {
//Gets the X,Y of the PictureBox in a System::Drawing::Point
Point p = pictureBox1->PointToClient(Cursor->Position);

//Displays the X,Y Point in a Label
this->lbl1->Text = p.ToString();

//Converts System::Drawing::Point to System::String
String^ pnt = p.ToString();

//Pulls out X as System::String (only works of X is 3 digits)
String^ xpnt = pnt->Substring ( 3 , 3 );
//Pulls out Y as System::String (only works of X & Y are 3 digits)
String^ ypnt = pnt->Substring ( 9 , 3 );

//Converts X and Y Strings to ints
char xc[32];
char yc[32];
int x,y;
x = sprintf_s (xc, 32, "%d", xpnt);
y = sprintf_s (yc, 32, "%d", ypnt);

//Pulls the image out of the picturebox
Image^ i = pictureBox1->Image;

// Builds a new bitmap out of the image
Bitmap^ b = gcnew Bitmap(i);

//Gets the pixel color at coordinates x, y
Color c = b->GetPixel(x,y);

// Here we simply show the color structure, but you can pull the RGB out of it as needed
MessageBox::Show(c.ToString());

			 }


Was This Post Helpful? 0
  • +
  • -

#5 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5207
  • View blog
  • Posts: 13,955
  • Joined: 18-April 07

Re: Need help with getting color of Pixel

Posted 16 February 2009 - 11:35 PM

no no no... come on man, the reason the color is coming out as Color [A=255, R=255, G=255, B=255] is because you are printing a color structure, that is the string representation.

Type "c" followed by the period and suddenly you will see all the methods and properties of the color object "c" which will allow you access to each color value etc. Don't parse the string.

Also if your image is 600 x 400, just make sure your X and Y don't have values outside the range of the picture.

We are not talking rocket science here. ;)
Was This Post Helpful? 0
  • +
  • -

#6 Gypsy09   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 09

Re: Need help with getting color of Pixel

Posted 17 February 2009 - 12:27 AM

Presto!!! It all works :D :^:

	private: System::Void pictureBox1_Click(System::Object^  sender, System::EventArgs^  e) {

// Gets the X,Y of the PictureBox in a System::Drawing::Point
Point p = pictureBox1->PointToClient(Cursor->Position);

// Displays the X,Y Point in a Label
this->lbl1->Text = p.ToString();

// Converts X and Y Strings to ints for use 
// in System::Drawing::Bitmap::GetPixel(int x,int y)
int x = p.X;
int y = p.Y;

// Pulls the image out of the picturebox
Image^ i = pictureBox1->Image;

// Builds a new bitmap out of the image
Bitmap^ b = gcnew Bitmap(i);

// Gets the pixel color at coordinates x, y
Color c = b->GetPixel(x,y);

// Change the color of the Label's BackColor to Color of Selected Pixel
this->lbl1->BackColor = c.FromArgb(c.R,c.G,c.B);

			 }



Thanks for all the Help!

Side Note: This will only work if you have your PictureBox Set to AutoSize. You could do a StretchImage but you will have to figure out the X,Y of the Actual Size of the image.
For example:
If the Actual Size of the Picture is 100x100 and your Picturebox set to 50x50 then just multiply your X and Y ints by 2 because your Picture is 50% of the original size.

This post has been edited by Gypsy09: 17 February 2009 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1