Getting pixel colors from another application's window

Posted on 2003-11-07
Last Modified: 2010-05-01

I need to be able to get the colors of pixels in another application's window. Currently i use the GetPixel API in my vb app and this works quite well. The problem is that GetPixel fails if the window your getting values from is minimised or covered etc. Does anybody know how i can get pixel color values from another window even if it is minimised? i have been searching for ages for a better way than Getpixel but no luck yet.

Question by:Redux

Expert Comment

ID: 9701942

As you found out, GetPixel will only get the color of an on-screen pixel.  If an app is minimzed or hidden, it's not on-screen!   The application is responsible for repainting the window when it is restored or maximized, so there really isn't a pixel color on the window when it is not visible.

Don't know if this helps, but you could probably use the API GetDC() to get the device context for the window (I'm assuming you have the window's handle), then use GetBkColor() to get the background color for that context.  This will return the default background color for the device context, but if the app draws on top of the background, the pixel color is not guaranteed.  

I hate to ask why you need to get the color of a pixel on a hidden window...!


Author Comment

ID: 9702314
Thanks for your reply. Yes, I have the handle of the window, and i have been getting the DC using the GetWindowDC API. Unfortunately your suggestion of GetBkColor wont work, because I need to read quite a few pixel colors at different coordinates not just the background color.

As for why I need to do this it is because my program is designed to operate in the background whilst the user is doing other things (hence both my app and the app im getting pixel colors from will be minimised).
LVL 17

Expert Comment

ID: 9704820
The device context is destroyed when not on-screen so getting it won't help either if it's minimized. A sloppy choice for you would be to set the window position to "on top", get the DC, copy it into a compatible bitmap/dc in your application then set the window back to it's original state. The showing/restoring should be only a flicker.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Author Comment

ID: 9706802
    The problem is I am getting calling GetPixel roughly 3 times a second as the pixel colors are changing every so often. So it might not be very pratical to constantly keep pulling the window to the top. Thanks for reply tho. Im increasing the points to 300 as it seems this is a very difficult question. Can anyone else think of a way to read individual pixels on minimised windows? If not I will award the points to zzzzzooc as he seems to have offered a possible solution that would work.

Expert Comment

ID: 9707633
Get the snap shot of the window before its minimized and then scan the pixels of the snap shot.


Author Comment

ID: 9707684
I'm sorry. I don't think i've explained this very well. The program im reading pixels from is displaying numbers on its window. My program uses getpixel to do a basic character recognition to find out what the numbers are and this works fine until the window is minimised or the areas im getpixelling from are covered. The numbers are also changing quite often which means i have to getpixel quite frequently. Obviously this means that i have to be able to getpixel (or some similar method which can retrieve pixel colors) lots of times, not just once. I notice that getpixel wont work on areas of windows that are not in the current clipping area (like obscured parts of the form, as windows just doesnt draw them). Could I maybe alter the clipping region so getpixel works on areas of the window hidden?
If what i'm trying to do isn't possible (get values of pixel colors not currently displayed) then please tell me.
LVL 17

Accepted Solution

zzzzzooc earned 300 total points
ID: 9708869
Only other alternative i've encountered is with WM_PAINT (have never attempted it myself yet):

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question