Getting pixel colors from another application's window


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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


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

ReduxAuthor Commented:
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).
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.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

ReduxAuthor Commented:
    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.
Get the snap shot of the window before its minimized and then scan the pixels of the snap shot.

ReduxAuthor Commented:
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.
Only other alternative i've encountered is with WM_PAINT (have never attempted it myself yet):

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.