Zaei
Feb 27th, 2001, 12:48 PM
Just a quick question. Can you use GetPixel with an offscreen DC?
Well, ive found that it will return -1 if i use a hidden picture box. I'm wondering if it will work if i use an offscreen DC
Fox
Feb 27th, 2001, 04:18 PM
It will.
If you want to use a picturebox set autoredraw to true..
kedaman
Feb 27th, 2001, 06:18 PM
Same problem appears for any DC if there's no bitmap selected.
plenderj
Feb 28th, 2001, 11:07 AM
Zaei, just create a DC in memory.
I do it in the source code I released/posted in the misc.bas module.
- jamie
Ok, from my experiments, you CANNOT use GetPixel
with an offscreen DC. You can Blt from one, which I
tested, but using GetPixel returns 0 (and when the pixel
referenced is invalid, it returns -1).
Mad Compie
Feb 28th, 2001, 01:39 PM
I made a memory DC and did a BitBlt of the desktop to it.
Using a For..Next loop with GetPixel() resulted in the colours and not 0 or -1!
Hrmmm.. Post code, please?
Mad Compie
Mar 2nd, 2001, 02:27 PM
Well, here it is my friend:
Option Explicit
'This project needs 1 picturebox
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function SetPixelV Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Dim W As Long
Dim H As Long
Dim memDC As Long
Dim memBMP As Long
Private Sub Form_Activate()
Me.Refresh
BitBlt Me.hdc, 0, 0, W, H, memDC, 0, 0, vbSrcCopy
End Sub
Private Sub Form_DblClick()
Unload Me
End Sub
Private Sub Form_Load()
W = Screen.Width
H = Screen.Height
Picture1.ScaleMode = vbPixels
Picture1.BackColor = 0
Me.ScaleMode = vbPixels
Me.Move 0, 0, W, H
W = W / Screen.TwipsPerPixelX
H = H / Screen.TwipsPerPixelY
Picture1.Move W - Picture1.Width, H - Picture1.Height
memDC = CreateCompatibleDC(Me.hdc)
memBMP = CreateCompatibleBitmap(Me.hdc, W, H)
SelectObject memDC, memBMP
BitBlt memDC, 0, 0, W, H, GetDC(0), 0, 0, vbSrcCopy 'Capture desktop...
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim C As Long
Dim i As Integer
Static BUSY As Boolean
If (BUSY) Then Exit Sub
BUSY = True
If (x < Picture1.Width) And (y < Picture1.Height) Then
For i = 0 To 9
'Get the colour in MEMORY!!! not from the Form!!!
C = GetPixel(memDC, x + i, y)
SetPixelV Picture1.hdc, x + i, y, C
C = GetPixel(memDC, x + i, y + 1)
SetPixelV Picture1.hdc, x + i, y + 1, C
Next i
End If
BUSY = False
End Sub
Private Sub Form_Unload(Cancel As Integer)
DeleteDC memDC
DeleteObject memBMP
Set Form1 = Nothing
End
End Sub