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.

This code is confusing, that's your main problem. I don't understand the need for POINT lame; Also, line 40 is useless as i 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.

Edited 7 Years Ago by William Hemsworth: n/a

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:

Edited 7 Years Ago by evilguyme: n/a

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) );
    }
  }
}

Edited 7 Years Ago by William Hemsworth: n/a

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.

Edited 7 Years Ago by William Hemsworth: n/a

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...

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...

Edited 7 Years Ago by evilguyme: n/a

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

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.

Edited 7 Years Ago by evilguyme: n/a

>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.

Edited 7 Years Ago by William Hemsworth: n/a

Comments
Still, good post
This article has been dead for over six months. Start a new discussion instead.