Getting pixel colors from another application's window

Posted on 2003-11-07
Medium Priority
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 900 total points
ID: 9708869
Only other alternative i've encountered is with WM_PAINT (have never attempted it myself yet):


Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

612 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