Read pixels from other apps windows

S

Stacey

I've been trying for the last couple of days to read some pixel values
from a window of an independent application, but, even though I got
close, I just can't figure the last part out.

I am able to read the coordinates (GetWindowRect), but the GetPixel
always returns some weird incremental value. Is there something wrong
with my declarations, or do I need a different handle to access the
pixels of a window, as opposed to just the coordinates?


Here's the code I'm using:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim proctest As Process()
Dim hMW As IntPtr
Dim pix, xy, t, r, b, l As Long
Dim wR As New RECT

proctest = Process.GetProcessesByName("[process name]")

hMW = proctest(0).MainWindowHandle
GetWindowRect(hMW, wR) ' the data returned here is correct
Debug.Write(wR.left & ", " & wR.top & ", " & wR.right & ", " &
wR.bottom)

For xy = 50 To 100
pix = GetPixel(hMW.ToInt64, xy, xy)
Debug.Write(xy & ":" & Hex(pix) & vbCrLf)
' here I always get 32FFFFFFFF to 64FFFFFFFF...
Next

End Sub

Public Structure RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Structure

Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect"
(ByVal hWnd As IntPtr, ByRef rect As RECT) As IntPtr
Declare Function GetPixel Lib "gdi32" (ByVal hWnd As Long, ByVal X As
Integer, ByVal Y As Integer) As Long
 
B

Bob Powell [MVP]

The code in Windows Forms Tips and Tricks for capturing a screenshot can be
used to capture small bitmaps or even single screen pixels. I've used it a
couple of times for eyedropper type applications.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
S

Stacey

Unfortunately, I'm not very good at C#, so I'm having a hard time
converting your code into VB.NET...
 
H

Herfried K. Wagner [MVP]

Stacey said:
I am able to read the coordinates (GetWindowRect), but the GetPixel
always returns some weird incremental value. Is there something wrong
with my declarations, or do I need a different handle to access the
pixels of a window, as opposed to just the coordinates?

\\\
Public Declare Function GetPixel Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nXPos As Int32, _
ByVal nYPos As Int32 _
) As Int32

Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr _
) As IntPtr

Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As IntPtr, _
ByVal hdc As IntPtr _
) As Int32

Private m_hdc As IntPtr

Private Sub Form1_Load( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Load
m_hdc = GetDC(IntPtr.Zero)
Me.Timer1.Enabled = True
End Sub

Private Sub Form1_Closed( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles MyBase.Closed
ReleaseDC(IntPtr.Zero, m_hdc)
End Sub

Private Sub Timer1_Tick( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles Timer1.Tick
Me.Label1.BackColor = _
ColorTranslator.FromWin32( _
GetPixel(m_hdc, Cursor.Position.X, Cursor.Position.Y) _
)
End Sub
///
 
S

Stacey

Hey, thanks!

I was able to integrate that into my existing code without a hitch, and
everything works as planned.

Now I just have to work on understanding it... ;)
\\\
Public Declare Function GetPixel Lib "gdi32.dll" ( _
ByVal hdc As IntPtr, _
ByVal nXPos As Int32, _
ByVal nYPos As Int32 _
) As Int32
[...]
 
H

Herfried K. Wagner [MVP]

Stacey,

Stacey said:
I was able to integrate that into my existing code without a hitch, and
everything works as planned.

Now I just have to work on understanding it... ;)

The mistake in your code is that you are passing a window handle ('HWND') to
'GetPixel' instead of passing a device context handle ('HDC'). My code gets
a handle to the desktop's device context (thus we use 'IntPtr.Zero' as the
window handle) and passes this handle to 'GetPixel'. I hope this makes
things more clear.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top