Problem with GetPixel - help please
The routine I have below I want to get the pixel of the screen. It returns nothing here. If I move the GetPixel call out of the loop, it returns something.
Any idea what I'm doing wrong or a better way of this?
Thanks!
VB Code:
Private Sub Command1_Click()
Dim hDC As Long
Dim H1 As Long
Dim W1 As Long
Dim Z As Long
H1 = Screen.Height / Screen.TwipsPerPixelY
W1 = Screen.Width / Screen.TwipsPerPixelX
Dim X As Long
Dim Y As Long
hDC = GetDC(0) 'get desktop dc
For Y = 1 To Y1 - 10
For X = 1 To H1 - 10
Z = GetPixel(hDC, X, Y)
Memo1.Text = Memo1.Text & Trim(Str(Z))
Next X
Next Y
End Sub
Re: Problem with GetPixel - help please
you're using Y1 rather than W1 (or whatever it should be) in the first For - use Option Explicit at the top of the form to catch these errors!
Re: Problem with GetPixel - help please
Also, when using GetDC(), you need to use ReleaseDC() when you're done with the DC. That allows the DC to be used again.
VB Code:
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
'ex. in your sub, when you're done with the DC
Call ReleaseDC(Me.hwnd, hdc)