954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

[HELP-CODE]Scanning Screen for pixel color

so far i have this code..

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <string>


using namespace std;

int main ()
{
	const int r1 = 175;
	const int g1 = 163;
	const int b1 = 134;

	HWND hwnd = FindWindow(NULL,"Test - Microsoft Visual Studio");
	SetForegroundWindow(hwnd);
	HDC hdc = GetDC(hwnd);
		
	POINT lame;
	lame.x = 0;
	lame.y = 0;

	int x = lame.x;
	int y = lame.y;


	for (int i = 0; i < 1280; i++)
	{
		x = i;
		y = 0;

		COLORREF col = GetPixel(hdc,lame.x,lame.y);

		int red1 = GetRValue(col);
		int green1 = GetGValue(col);
		int blue1 = GetBValue(col);



		if (x == 1280)
		{
			y = lame.y++;
			i = 0;
		}

		if (y == 801)
		{
			system("pause");
		}

		if (red1 == r1 && green1 == g1 && blue1 == b1)
		{
			cout << "Pixel found at: \t" << "X: "<< x << "\t\t" << "Y: " << y << "\n";
			system("pause");
		}
	}
	system("pause");
	return 0;


	
}


i use it for scanning for a certain pixel on the screen but it doesn't seem to be working... no compiler or runtime errors but it just skips straight to press any key to continue...

i also can't find any resources to teach me some bitmap way..
if someone would care to explain how to capture screen to bmp and then analyze it for pixels..

or just tell me where i've gone wrong in my code.

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This code is confusing, that's your main problem. I don't understand the need for POINT lame; Also, line 40 is useless asi will never be equal to 1280 (only ever < than 1280).

If you want to loop through every pixel from point (0,0) and (1280,801) to find a pixel, do this:

/* Untested code */
COLORREF tofind = RGB(175, 163, 134);
COLORREF col;

for (int y = 0; y < 801; ++y) {
  for (int x = 0; x < 1280; ++x) {
    col = GetPixel( hdc, x, y );
    if ( col == tofind ) {
      cout << "Pixel found at: \t" << "X: "<< x << "\t\t" << "Y: " << y << "\n";
      cin.ignore();
    }
  }
}

cin.ignore();


Hope this helps.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

lemme try that. thnx for the code :)


that still doesn't work...

still exits program at the end.

so i added

system("pause");


and now it goes straight to press any key to continue...

i tried setting the color to white (all 0's) and still takes me to the end .. no pixel found.
even at all set at 255 = black it couldn't find anything D:

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

One thing you may want to bear in mind, the GetPixel function is very slow, so you could literally be waiting 10 minutes before it finds the pixel. To demonstrate just how slow... try executing this code.

int main() {
  COLORREF tofind = RGB(255, 0, 0);
  COLORREF col;

  HDC hdc = GetDC(NULL);

  for (int y = 0; y < 801; ++y) {
    for (int x = 0; x < 1280; ++x) {
      col = GetPixel( hdc, x, y );
      SetPixel( hdc, x, y, RGB(255,0,0) );
    }
  }
}
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

hmm ... then what's a good alternative to GetPixel()?

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 
hmm ... then what's a good alternative to GetPixel()?

Learn how to use bitmaps, take a screenshot, and lock the bitmap pixels to gain fast access to them.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

oooh that way...

hmm there doesn't seem to be a perfect tut out there..

i understand how to create an object of the bitmap and use btblts or whatever that API is.

but then how do i scan it for pixels?

google dint help...

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

> google dint help

???
It's a win32 FAQ for about 15 years (!)
See on Win32 grp for classic code
to scan very quickly pixels with GDI or GDI+

marco93
Junior Poster
132 posts since Apr 2008
Reputation Points: -76
Solved Threads: 14
 

it gives me a page not found error..

maybe u can tell me what strings to search for in google? :P

cuz i searched for like 30 mins and dint find anything good... :O

edit : oh wait nvm i got the site to work.. but its hard navigating in that site... lemme try the search :P

edit 2: zomg!! i searched pixel there and got no results..

then i searched bitmap and got some 5 lame results...

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

i searched pixel there and got no results..

then i searched bitmap and got some 5 lame results...

Don't pay any attention to anything Marco93 says. We are still waiting for his first descent post.

Perhaps this can help you

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

oh kay thnx :)

aww man that stuff is harder for me cuz im still learning from a book and doing these projects to help me learn the stuff that's not in the book.

isn't there a simpler way.. cuz that guy is using it for game development..

is there some way where i don't have to use pointers as i haven't got to that part yet.

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

>Don't pay any attention to anything Marco93 says
He's right, see those 3 red dots by his reputation? As far as I know, he's the first person to ever accomplish that.

If you want to manage this using windows only, there is no easy way. I spent 15 minutes trying to figure it out, and this is as close as I got (there was a lot more, but didn't work very well):

#include <iostream>
#include <windows.h>
using namespace std;

int main() {
  // The pixel we're scanning for
  COLORREF tofind = RGB( 255, 0, 0 );

  // Screen size
  int screen_width = GetSystemMetrics( SM_CXSCREEN );
  int screen_height = GetSystemMetrics( SM_CYSCREEN );

  // Get HBITMAP from our HDC of the screen
  HDC hdc = GetDC( NULL );
  HDC hdcMem = CreateCompatibleDC( hdc );

  HBITMAP bmp = CreateCompatibleBitmap( hdcMem, screen_width, screen_height );
  SelectObject( hdcMem, bmp );
  BitBlt( hdcMem , 0, 0, screen_width, screen_height, hdc, 0, 0, SRCCOPY );

  // bmp now contains the screenshot
}


You're welcome to try and complete it, but remember it's not easy. Here are some links to help: [link] , [link] , [link] and most importantly... [link]

Hope this helps.

William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
 

thnx :D

evilguyme
Junior Poster in Training
71 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You