Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: reading the color of a pixel on the screen


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Oct 7, 2015 2:55 AM Last Post By: David Ayre
David Ayre

Posts: 34
Registered: 10/11/00
reading the color of a pixel on the screen  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 6, 2015 3:41 AM
Hi,

I want to be able to read the color of a pixel on the screen.
I am using a TImage panel.
I have tried all I can think of but with no success.
I am using BDS2006.

Thanks

David Ayre
Arkady Semylio

Posts: 87
Registered: 9/18/15
Re: reading the color of a pixel on the screen  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 6, 2015 5:08 AM   in response to: David Ayre in response to: David Ayre
David Ayre wrote:
Hi,

I want to be able to read the color of a pixel on the screen.
I am using a TImage panel.
I have tried all I can think of but with no success.
I am using BDS2006.

Thanks

David Ayre

Try this:

    TColor Color = clBlack;
    HDC hDC = 0;
    TCanvas* C = NULL;
    try {
        hDC = GetDC( GetDesktopWindow() );
        C = new TCanvas();
        C->Handle = hDC;
        Color = C->Pixels[1100][1100];
    }
    __finally {
        delete C;
        ReleaseDC( NULL, hDC );
    }
    ShowMessage( Format( "0x%.8X", ARRAYOFCONST( ( Color ) ) ) );


or


#include <memory>
 
    TColor Color = clNone;
    {
        struct DCRaii {
            explicit DCRaii( HDC hDC ) : hDC_( hDC ) {}
            ~DCRaii() { ::ReleaseDC( NULL, hDC_ ); }
            HDC GetDC() const { return hDC_; }
            HDC hDC_;
        }
        DCMngr( GetDC( GetDesktopWindow() ) );
        std::unique_ptr<TCanvas> C( new TCanvas() );
        C->Handle = DCMngr.GetDC();
        Color = C->Pixels[1100][1100];
    }
    ShowMessage( Format( "%.8X", ARRAYOFCONST( ( Color ) ) ) );
}


Bye
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: reading the color of a pixel on the screen  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 6, 2015 12:00 PM   in response to: Arkady Semylio in response to: Arkady Semylio
Arkady wrote:

hDC = GetDC( GetDesktopWindow() );

GetDesktopWindow() returns the HWND of the desktop window (the window hosting
the ListView for the desktop icons, the taskbar, etc). To get the HDC of
the screen itself, use GetDC(0) instead.

Color = C->Pixels[1100][1100];

The Pixels[][] property is just a wrapper for the Win32 GetPixel() function.
So if you are going to use the Win32 API directly to get the HDC, you may
as well use the Win32 API directly to get the pixel, too.

HDC hDC = GetDC(0);
TColor Color = (TColor) GetPixel(hDC, 1100, 1100);
ReleaseDC(0, hDC);
ShowMessage( Format( "0x%.8X", ARRAYOFCONST( ( Color ) ) ) );


ReleaseDC( NULL, hDC );

When calling ReleaseDC(), you have to specify the same HWND that you specifiy
to GetDC(). If you use GetDesktopWindow() in GetDC(), NULL in ReleaseDC()
will leak the HDC.

--
Remy Lebeau (TeamB)
David Ayre

Posts: 34
Registered: 10/11/00
Re: reading the color of a pixel on the screen  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 7, 2015 2:55 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks both of you. What I was doing wrong was to
try to put the two coordinates into one set of brackets.

Cheers,

David
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02