in reply to Detect Pixel Color on WinXP

I'm trying to expand my knowledge of the Windows API. Why? I'm the curious type and I like a good challenge. So I went ahead and read the docs and came up with this.

#!/usr/bin/perl use strict; use warnings; use Win32::API; #Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As L +ong Win32::API->Import( 'user32', 'long GetDC(long hWnd)', ); #Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As +Long, ByVal x As Long, ByVal y As Long) As Long Win32::API->Import( 'gdi32', 'long GetPixel(long hdc, long x, long y)', ); sub PixelColor { my ($x, $y) = @_; my $dc = GetDC(''); my $rgb = sprintf "%06x", GetPixel(GetDC(''), $x, $y); return $rgb; } print PixelColor(100, 100), "\n";

Update: Changed "%x" to "%06x".

Replies are listed 'Best First'.
Re: Re: Detect Pixel Color on WinXP
by bart (Canon) on Sep 16, 2003 at 00:09 UTC
    You forgot the call to ReleaseDC(). You might just have created a huge memory leak. And what on earth are you trying to pass on as a hWnd? That's the ID (window handle) of the control/window you're trying to access, so try to get a valid value. If you want to access the desktop as a window, you can use the GetDesktopWindow() API call, which will return the hWnd of the mother of all windows, the desktop. You can proceed from there. These are the additional/changed code snippets:
    #Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, +ByVal hDC As Long) As Long Win32::API->Import( 'user32', 'long ReleaseDC(long hWnd, long hDC)', ); #Private Declare Function GetDesktopWindow Lib "user32" () As Long Win32::API->Import( 'user32', 'long GetDesktopWindow()', ); sub PixelColor { my ($hwnd, $x, $y) = @_; my $dc = GetDC($hwnd); my $rgb = GetPixel($dc, $x, $y); ReleaseDC($hwnd, $dc); return $rgb < 0 ? undef: sprintf "%06x", $rgb; } print PixelColor(GetDesktopWindow(), 100, 100);
    Well, that's the theory. In practice I always get "ffffff" undef back, which means this pixel can't be read — likely because the pixel data isn't buffered. Oh well. Perhaps it works better with a hWnd of an actual window...

    p.s. This hWnd should be a property of your control or canvas, or whatever terminology you might be used to.

    p.p.s. For people who feel like trying this out, make sure you have at least version 0.40 of Win32::API, or you'll get a deep recursion error on the Import method — something to do with AUTOLOAD, because that method doesn't exist. Needless to say, it won't work, either.

      I'm just following the MS documentation on Get DC (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/devcons_4esj.asp):
      HDC GetDC(<br /> HWND hWnd // handle to window<br /> );
      < Parameters
      hWnd
      [in] Handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen.


      I missed the part about ReleaseDC so thanks for the heads up. I suspect that you are getting undef because of your call to GetDesktopWindow.

      Here is the code I used to test with the ReleaseDC and check for negative numbers added in.:

      #!/usr/bin/perl use strict; use warnings; use Win32::API; Win32::API::Struct->typedef( POINT => qw( LONG x; LONG y; ) ); Win32::API->Import( 'user32' => 'BOOL GetCursorPos(LPPOINT pt)' ); Win32::API->Import( 'user32' => 'long GetDC(long hWnd)' ); Win32::API->Import( 'gdi32' => 'long GetPixel(long hWnd, long x, long + y)' ); Win32::API->Import( 'user32' => 'long ReleaseDC(long hWnd, long hDC)' +); sub PixelColor { # if $hwnd is null or '', act on the entire screen my ($hwnd, $x, $y) = @_; my $dc = GetDC($hwnd); my $rgb = GetPixel($dc, $x, $y); ReleaseDC($hwnd, $dc); return $rgb < 0 ? undef: sprintf "%06x", $rgb; } my $pt = Win32::API::Struct->new( 'POINT' ); while (1) { GetCursorPos($pt) or die "GetCursorPos failed: $^E"; print "Cursor is at: $pt->{x}, $pt->{y}\n"; print "The pixel color is: ", PixelColor('',$pt->{x}, $pt->{y}), "\n +"; sleep(2); }

      Updated: Added code and suspicion of cause of bart's problem with the code, so go back and reread this entire post. :-)