# GetPixel function

Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last
• 07-04-2007
Muzzy A
ok so if i wanted to check if a pixel is white, what would i have to do? I dont understand the numbers GetPixel returns. Like "13026246" - its not enough numbers to be an RGB value and "4294967295" - thats to many numbers to be an RGB value. I really don't get it lol
• 07-04-2007
swoopy
Well if you looked at the link Indigo posted, it shows you how to interpret a COLORREF. The lower eight bits are for red, the next eight for green, and the next eight are blue. The last eight are zero. So one way to break up the parts would be to use bit shifting. Once you do this, each part (red, green, and blue) will be a value from 0 to 255.
Code:

```COLORREF color = GetPixel(hdc, x, y); unsigned int r = color & 0xff; unsigned int g = (color & 0xff00) >> 8; unsigned int b = (color & 0xff0000) >> 16; cout << "red: " << r << endl; cout << "green: " << g << endl; cout << "blue: " << b << endl;```
I think there are also some window macros which will break up a COLORREF. Or you could also use the bitset class from the standard library to make things easier.
• 07-04-2007
swoopy
>ok so if i wanted to check if a pixel is white, what would i have to do?
White would be a 255 for all three components (red=255, green=255, blue=255). Red of course would be (red=255, green=0, blue=0). So to check for white using the above code:
Code:

```if (r == 255 && g == 255 && b == 255) {     //Pixel is white }```
Or you could also write:
Code:

```if (color == 0xffffff) {     //Pixel is white }```
• 07-04-2007
Muzzy A
Code:

`0xffffff`
it doesnt work, neither does
Code:

`0x000000`
for black

and for some reason r g b are all getting the value 255 every time no matter what.

do you know what could cause this?
• 07-04-2007
Muzzy A
[EDIT] sorry for the double post
• 07-04-2007
Yarin
COLORREF is a waste of finger time, just use DWORD. :) (It's the same thing. ;))

Try this instead: It may mork and may not, so hey...
Code:

```DWORD color = GetPixel(hdc, x, y); unsigned int r = GetRValue(color); unsigned int g = GetGValue(color); unsigned int b = GetBValue(color); cout << "red: " << r << endl; cout << "green: " << g << endl; cout << "blue: " << b << endl;```
• 07-04-2007
Muzzy A
its the same thing with that to :S maybe if you all look at my code something may catch your eye that im doing wrong lol.

Code:

```int x,y,SX=347,SY=375;         unsigned char pixels[30][100];         unsigned int r,g,b;         HDC hdc = GetDC(NULL);         DWORD color=GetPixel(hdc,x,y);         for(y=SY;y<SY+30;y++)         for(x=SX;x<SX+100;x++)         {         color;         r=GetRValue(color);         g=GetGValue(color);         b=GetBValue(color);         if(r==255 && g==255 && b==255)                           cout << "white ";     else if(r==0 && g==0 && b==0)                       cout << "black ";     }```
• 07-04-2007
Yarin
Hmm, what's this? ;)
Code:

```int x,y,SX=347,SY=375;         unsigned char pixels[30][100];         unsigned int r,g,b;         HDC hdc = GetDC(NULL);         DWORD color=GetPixel(hdc,x,y);         Sleep(2000);         for(y=SY;y<SY+30;y++)         for(x=SX;x<SX+100;x++)         {         color;         r=GetRValue(color);         g=GetGValue(color);         b=GetBValue(color);         if(r==255 && g==255 && b==255)                           cout << "white ";     else if(r==0 && g==0 && b==0)                       cout << "black ";     }```
• 07-04-2007
Muzzy A
that does the GetPixel() function doesnt it?
• 07-04-2007
Yarin
No, this does:
Code:

```int x,y,SX=347,SY=375;         unsigned char pixels[30][100];         unsigned int r,g,b;         HDC hdc = GetDC(NULL);         DWORD color=GetPixel(hdc,x,y);         Sleep(2000);         for(y=SY;y<SY+30;y++)         for(x=SX;x<SX+100;x++)         {         color;         r=GetRValue(color);         g=GetGValue(color);         b=GetBValue(color);         if(r==255 && g==255 && b==255)                           cout << "white ";     else if(r==0 && g==0 && b==0)                       cout << "black ";     }```
You both create and assign color on the same line.
• 07-04-2007
Muzzy A
ah ok i see your point, i'll give it a shot.
• 07-04-2007
Yarin
What are you trying to get an image of anyway? If it's another window then you need to set your hdc to it's hdc, and start getting the pixels at 0x0.
• 07-05-2007
Muzzy A
i fixed it here's what ive got, and ya im getting the pixel values of an image in a browser

Code:

```int x,y,pixel=-1,wpix,offset;         unsigned char pixels[30][100];         unsigned int r,g,b;         HDC hdc = GetDC(NULL);         DWORD color;         Sleep(3000);         for(y=SY;y<SY+30;y++)                                                   for(x=SX;x<SX+100;x++)                                                   {                                               pixel=pixel+1;                                                   color=GetPixel(hdc,x,y);                                                   r=GetRValue(color);                                                   g=GetGValue(color);                                                   b=GetBValue(color);                                                   if(r==255 && g==255 && b==255)                                                     // white pixel```
• 07-05-2007
swoopy
Quote:

Originally Posted by Yarin
COLORREF is a waste of finger time, just use DWORD. :) (It's the same thing. ;))

Until the definition of DWORD or COLORREF changes.
• 07-05-2007
Yarin
Code:

`pixel=pixel+1;`
can be
Code:

`pixel += 1;`
:)
Show 80 post(s) from this thread on one page
Page 2 of 3 First 123 Last