Computer Science Canada

Need a quick help on getting the RGB of a pixel.

Author:  HazySmoke)345 [ Sat Nov 19, 2005 7:47 pm ]
Post subject:  Need a quick help on getting the RGB of a pixel.

I'm trying to get the color of a pixel on the top left corner of the screen, so I typed

code:
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long

Private Sub Command1_Click()
    Print GetPixel(Me.hdc, 0, 0)
End Sub


But the outcome is 13554646 always. Now, I'm not that clear about what I'm doing here... does "Me.hdc" confine the program to find the color of the pixel inside the form? If it does... how do I make it find the top left corner of the whole screen?

Author:  Brightguy [ Sat Nov 19, 2005 10:31 pm ]
Post subject:  Re: Need a quick help on getting the RGB of a pixel.

Yes, Me.hdc will only return the device context handle to the form.

I'm sure there are lots of ways to do this (e.g. CreateDC) but GetWindowDC seems to work fine:
VisualBASIC:
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long

Private Sub Form_Click()
    Me.BackColor = GetPixel(GetWindowDC(0), 0, 0)
End Sub


: