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.

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:

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

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

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.

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

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

commented: Another -6
commented: Yawn. -3

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

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.

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

commented: Still, good post +32

thnx :D

You are on the right track evilguyme.

Try this:

    HDC hDC = GetDC(0);

    // color you are looking for
    const int r1 = 255;
    const int g1 = 255;
    const int b1 = 255;

    //HWND hwnd = FindWindow(NULL,"Test - Microsoft Visual Studio");
   //SetForegroundWindow(hwnd);
    HDC hdc = GetDC(HWND_DESKTOP);
commented: You have neatly explained why progress is slow. 9 years for a step forward. +15
   I've used this code for over a decade. I added the SetPixel option to put a red pixel where it checks. Also SetCursor for added features to play with. Useful stuff. Slow as stated for full screen scanning. 

 /* Pixel scanner
         Joshua D. Haglund
         Visual Studio 2017+*/

    int main() {

        //don't ask me about long ints
        long int CURSOR_X_scan = 0;
        long int CURSOR_Y_scan = 0;
        long int CURSOR_X = 0;
        long int CURSOR_Y = 0;
        long int check=0;
        long int x, y;

            POINT p;
            GetCursorPos(&p);

            x = p.x;
            y = p.y;    

                // check long enough for 4k resolutions
            while (check != 8294401) {
                // change scan ranges if desired
            for (long int CURSOR_Y_scan = 0; CURSOR_Y_scan < 2160; CURSOR_Y_scan++) {
            for (long int CURSOR_X_scan = 0; CURSOR_X_scan < 3840; CURSOR_X_scan++) {

                    GetCursorPos(&p);

                    x = p.x;
                    y = p.y;

                    HDC hDC = GetDC(HWND_DESKTOP);  

                    COLORREF rgb;

                    long int CMP_R = 0;
                    long int CMP_G = 0;
                    long int CMP_B = 0;

                    BYTE rVal;
                    BYTE gVal;
                    BYTE bVal;

                    long int red;
                    long int green;
                    long int blue;

                    rgb = GetPixel(hDC, x, y);

                    DWORD color = GetPixel(hDC, x, y);

                        rVal = GetRValue(rgb);
                        gVal = GetGValue(rgb);
                        bVal = GetBValue(rgb);

                        red = (long int)rVal;
                        green = (long int)gVal;
                        blue = (long int)bVal;

                        unsigned long int r = GetRValue(color);
                        unsigned long int g = GetGValue(color);
                        unsigned long int b = GetBValue(color);

                        long int CURSOR_X_scan = 0;
                        long int CURSOR_Y_scan = 0;
                        long int CURSOR_X = 0;
                        long int CURSOR_Y = 0;

                    // may need ... ALT+F4 lol 
                    //  SetCursorPos(CURSOR_X_scan, CURSOR_Y_scan); // sets cursor to scan x,y cords (cannot move from spot)

                        color = GetPixel(hDC, x, y);

                    //  SetPixel(hDC, x, y, RGB(255, 0, 0)); // marks spot red where checked.

                        cout << x << " " << y << " " << endl; 
                        cout << "RED: " << r << " " << "GREEN: " << g << " " << "BlUE: " << b << " " << "Checks: " << check << endl;

                        if (r == 0 && g == 0 && b == 0) {
                            cout << " black spotted!" << endl;
                        }
                        if (r == 255 && g == 255 && b == 255) {
                            cout << " white spotted!" << endl;
                        }
                        if (r > 0 && r < 255 && g >0 && g < 255 && b > 0 && b < 255) {
                            cout << " unknown spotted!" << endl;
                        }
                        check = check + 1;
                    }

                }
            }

            }
            return 0;
            }
commented: Welcome time traveler from 2009. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.