943,901 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1823
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 27th, 2009
0

[HELP-CODE]Scanning Screen for pixel color

Expand Post »
so far i have this code..

C++ Syntax (Toggle Plain Text)
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <windows.h>
  4. #include <string>
  5.  
  6.  
  7. using namespace std;
  8.  
  9. int main ()
  10. {
  11. const int r1 = 175;
  12. const int g1 = 163;
  13. const int b1 = 134;
  14.  
  15. HWND hwnd = FindWindow(NULL,"Test - Microsoft Visual Studio");
  16. SetForegroundWindow(hwnd);
  17. HDC hdc = GetDC(hwnd);
  18.  
  19. POINT lame;
  20. lame.x = 0;
  21. lame.y = 0;
  22.  
  23. int x = lame.x;
  24. int y = lame.y;
  25.  
  26.  
  27. for (int i = 0; i < 1280; i++)
  28. {
  29. x = i;
  30. y = 0;
  31.  
  32. COLORREF col = GetPixel(hdc,lame.x,lame.y);
  33.  
  34. int red1 = GetRValue(col);
  35. int green1 = GetGValue(col);
  36. int blue1 = GetBValue(col);
  37.  
  38.  
  39.  
  40. if (x == 1280)
  41. {
  42. y = lame.y++;
  43. i = 0;
  44. }
  45.  
  46. if (y == 801)
  47. {
  48. system("pause");
  49. }
  50.  
  51. if (red1 == r1 && green1 == g1 && blue1 == b1)
  52. {
  53. cout << "Pixel found at: \t" << "X: "<< x << "\t\t" << "Y: " << y << "\n";
  54. system("pause");
  55. }
  56. }
  57. system("pause");
  58. return 0;
  59.  
  60.  
  61.  
  62. }

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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
evilguyme is offline Offline
70 posts
since Sep 2009
Sep 27th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

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:
C++ Syntax (Toggle Plain Text)
  1. /* Untested code */
  2. COLORREF tofind = RGB(175, 163, 134);
  3. COLORREF col;
  4.  
  5. for (int y = 0; y < 801; ++y) {
  6. for (int x = 0; x < 1280; ++x) {
  7. col = GetPixel( hdc, x, y );
  8. if ( col == tofind ) {
  9. cout << "Pixel found at: \t" << "X: "<< x << "\t\t" << "Y: " << y << "\n";
  10. cin.ignore();
  11. }
  12. }
  13. }
  14.  
  15. cin.ignore();
Hope this helps.
Last edited by William Hemsworth; Sep 27th, 2009 at 10:38 am.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Sep 27th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

lemme try that. thnx for the code


that still doesn't work...

still exits program at the end.

so i added

C++ Syntax (Toggle Plain Text)
  1. 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:
Last edited by evilguyme; Sep 27th, 2009 at 11:36 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
evilguyme is offline Offline
70 posts
since Sep 2009
Sep 27th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

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.
C++ Syntax (Toggle Plain Text)
  1. int main() {
  2. COLORREF tofind = RGB(255, 0, 0);
  3. COLORREF col;
  4.  
  5. HDC hdc = GetDC(NULL);
  6.  
  7. for (int y = 0; y < 801; ++y) {
  8. for (int x = 0; x < 1280; ++x) {
  9. col = GetPixel( hdc, x, y );
  10. SetPixel( hdc, x, y, RGB(255,0,0) );
  11. }
  12. }
  13. }
Last edited by William Hemsworth; Sep 27th, 2009 at 11:26 am.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Sep 27th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

hmm ... then what's a good alternative to GetPixel()?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
evilguyme is offline Offline
70 posts
since Sep 2009
Sep 27th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

Click to Expand / Collapse  Quote originally posted by evilguyme ...
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.
Last edited by William Hemsworth; Sep 27th, 2009 at 4:36 pm.
Reputation Points: 1429
Solved Threads: 129
Posting Virtuoso
William Hemsworth is offline Offline
1,542 posts
since Mar 2008
Sep 28th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

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...
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
evilguyme is offline Offline
70 posts
since Sep 2009
Sep 28th, 2009
-2

Re: [HELP-CODE]Scanning Screen for pixel color

> 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+
Last edited by marco93; Sep 28th, 2009 at 10:59 am.
Reputation Points: -76
Solved Threads: 14
Junior Poster
marco93 is offline Offline
132 posts
since Apr 2008
Sep 28th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

it gives me a page not found error..

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

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

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

then i searched bitmap and got some 5 lame results...
Last edited by evilguyme; Sep 28th, 2009 at 11:54 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
evilguyme is offline Offline
70 posts
since Sep 2009
Sep 28th, 2009
0

Re: [HELP-CODE]Scanning Screen for pixel color

Click to Expand / Collapse  Quote originally posted by evilguyme ...
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
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Makefile with multiple directories
Next Thread in C++ Forum Timeline: how to complile in code blocks





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC