Results 1 to 2 of 2

Thread: Problems using GetPixel() in VB.net 2005

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2007
    Posts
    1

    Problems using GetPixel() in VB.net 2005

    Ok, I am trying to read a single pixel's color from another application.

    I have used GetPixel before (way back with VB6). However, this time around, I can't get it to read correctly!

    Here is what I am TRYING to do:

    1. Get target window handle (hWnd) using FindWindow()
    2. Get device context (hDC) of window using GetDC(hwnd)
    3. Get Pixel color using GetPixel(hdc, x, y)
    4. ReleaseDC()


    However, GetPixel never returns a color. It either returns 'FFFFFF', 0, or -1.

    I don't know if it's my declarations, or what I'm doing wrong. I have been able to declare and use other API functions just fine- I just can't figure out how to get GetPixel() to work properly!

    I'm using VB.net / Visual Studio 2005. Here is the code that is in question:
    Code:
    Code:
        Declare Auto Function FindWindow Lib "user32" ( _
          ByVal lpClassName As String, _
          ByVal lpWindowName As String) As IntPtr
    
        Declare Function GetDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
        Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
    
        Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32) As Int32
    
        Public Function GetColorAt(ByVal X As Int32, ByVal Y As Int32) As Int32
    
            Dim hWnd As IntPtr
            Dim hDC As IntPtr
    
            hWnd = FindWindow(vbNullString, "RagII")
    
            hDC = GetDC(hWnd)
    
            Dim lColor As Int32 = GetPixel(hDC, X, Y)  
    
            ReleaseDC(hWnd, hDC)
    
            Return lColor
        End Function
    Does anyone know what I'm doing wrong?

    Thanks in advance,
    P.dub

  2. #2
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    Re: Problems using GetPixel() in VB.net 2005

    You could have problems if the pixel is part of a video. This may give you ideas - it displays the colour under the cursor and an RGB value as the forms' caption. Just put a timer on the form.
    Code:
    Option Strict Off
    Option Explicit On
    Imports VB = Microsoft.VisualBasic
    Imports System.Runtime.InteropServices
    
    Friend Class Form1
       Inherits System.Windows.Forms.Form
    
    <StructLayout(LayoutKind.Sequential)> Private Structure POINTAPI
       Dim x As Integer
       Dim y As Integer
    End Structure
    
    Private Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer
    Private Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As Integer) As Integer
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
    
    Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
       Timer1.Interval = 100
       Timer1.Enabled = True
    End Sub
    	
    Private Sub Timer1_Tick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Timer1.Tick
       Dim structCursorPosition As POINTAPI
       Call GetCursorPos(structCursorPosition)
       Dim lColor As Integer = GetPixel(GetDC(0), structCursorPosition.x, structCursorPosition.y)
       BackColor = System.Drawing.ColorTranslator.FromOle(lColor)
       Dim strRgb As String = VB.Right("000000" & Hex(lColor), 6)
       Text = "R:" & VB.Right(strRgb, 2) & " G:" & Mid(strRgb, 3, 2) & " B:" & VB.Left(strRgb, 2)
    End Sub
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width