I've looked everywhere. Given the coordinates of a point on the screen (such as 185,185), how do
you get the color of said point using VB.NET 2003.
I have looked EVERYWHERE for this, but to no avail. All the examples I've
seen use old code that is no longer supported in .NET.
For example, there are numerous places that have quoted the routine:
Private Type POINTAPI
X As Long
Y As Long
since TYPE is not supported in .NET 2003, it doesn't help.
I got most of the following code here from experts exchange, but it doesn't work- it just keeps
giving me the same dozen digit number over and over again (which has no
meaning to me). The Mouse Position works great. The code grabs the position of the mouse. But it never returns any color other than RGB= 127,254,3. AM I doing something wrong? I'm quite desperate.
Please remember, I'm on VB.NET 2003. Old VB code just doesn't work in this version
Public Class Form1
Public Structure POINTAPI
Public X As Long
Public Y As Long
Public Declare Function GetCursorPos Lib "USER32.dll" (ByVal lpPoint As POINTAPI) As Long
Public Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As Long, ByVal nXPos As Long, ByVal nYPos As Long) As Long
Public Declare Function ReleaseDC Lib "user32.dll" (ByVal hWnd As Long, ByVal hdc As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function GetDesktopWindow Lib "USER32.dll" () As Long
Private lDesktopDC As Long
#Region " Windows Form Designer generated code "
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
Dim mXY As POINTAPI, mHc As Long, rgbV As Long, mDC As Long
Dim R As Byte, G As Byte, B As Byte, rv, ry
Dim oXY As POINTAPI, HexVal As String
rv = Cursor.Position.X 'get mouse position X
ry = Cursor.Position.Y 'get mouse position Y
mHc = GetDesktopWindow() 'get a handle to screen
rgbV = GetPixel(GetDC(0), rv, ry)
'---convert color value from long to hex/rgb-----------------------
HexVal = Hex(Val(rgbV))
R = CLng("&H" + Mid(HexVal, 1, 2)) 'red color value
G = CLng("&H" + Mid(HexVal, 3, 2)) 'green color value
B = CLng("&H" + Mid(HexVal, 5, 2)) 'blue color value
TextBox1.Text = "RGB= " & R & "," & G & "," & B 'show RGB values
TextBox2.Text = rgbV
ReleaseDC(mHc, mDC) ' release device context
This code above does not work. My real goal is to be able to grab the color of a mouse location on another application. I'd like to do this:
GetPixel (x,y) 'return the color of a pixel on the other application
I've spent hours in goolge and on here looking for a way to do this, but all code examples I've found are in older versions of .NET and generate errors.