问题
I am trying to get the pixel color for set screen coordinates. I have a function I found and for some reason I can not call it. I placed it into a new class and I am trying to call it from my main form although it is not recognizing the function. It is a public class and a public function so I am not sure why. Thanks.
#Region "#include"
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
#End Region
Public Class Test
#Region "From Windows API"
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function GetWindowDC(ByVal hwnd As IntPtr) As IntPtr
'Do not try to name this method "GetDC" it will say that user32 doesnt have GetDC !!!
End Function
<DllImport("user32.dll", SetLastError:=True)> _
Public Shared Function ReleaseDC(ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As Int32
End Function
<DllImport("gdi32.dll", SetLastError:=True)> _
Public Shared Function GetPixel(ByVal hdc As IntPtr, ByVal nXPos As Integer, ByVal nYPos As Integer) As UInteger
End Function
#End Region
REM --Test--
#Region "Some Functions"
Public Function GetPixelColor(ByVal x As Integer, ByVal y As Integer) As Color
Dim hdc As IntPtr = GetWindowDC(IntPtr.Zero)
Dim pixel As UInteger = GetPixel(hdc, x, y)
Dim color As Color
ReleaseDC(IntPtr.Zero, hdc)
MsgBox(pixel)
color = color.FromArgb(Int(pixel And &HFF), _
Int(pixel And &HFF00) >> 8, _
Int(pixel And &HFF0000) >> 16)
Return color
End Function
#End Region
End Class
回答1:
To answer your question, you would either need to create an instance of that class:
Dim t As New Test
Dim pc As Color = t.GetPixelColor(20, 20)
Or make that function a Public Shared Function, then you would not need an instance of test:
Dim pc As Color = test.GetPixelColor(20, 20)
Alternately you could just take a screengrab and use the built in bitmap.GetPixel and avoid that class all together:
Dim screenSize As Size = New Size(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Using screenGrab As New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
Using g As Graphics = Graphics.FromImage(screenGrab)
g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenSize)
Dim pc As Color = screenGrab.GetPixel(20, 20)
End Using
End Using
回答2:
Public Class Form1
Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Declare Function GetDesktopWindow Lib "user32" Alias "GetDesktopWindow" () As Long
Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Long) As Long
Public Function GetPixelColor(ByVal x As Long, ByVal y As Long)
GetPixelColor = GetPixel(GetWindowDC(GetDesktopWindow), x, y)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'This is how often the timer refreshes in MILLISECONDS - i.e. 1000 = 1 second
Timer1.Interval = 2
Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim ScreenX As Integer
Dim ScreenY As Integer
Dim bm As Bitmap
' ScreenX = My.Computer.Screen.Bounds.Width Use this code instead, to capture the WHOLE SCREEN
' ScreenY = My.Computer.Screen.Bounds.Height Use this code instead, to capture the WHOLE SCREEN
'This code captures a CROPPED SECTION OF THE SCREEN - X COORDINATE
ScreenX = 460
'This code captures a CROPPED SECTION OF THE SCREEN - X COORDINATE
ScreenY = 80
bm = New Bitmap(ScreenX, ScreenY)
Using gr As Graphics = Graphics.FromImage(bm)
gr.CopyFromScreen( _
Screen.PrimaryScreen.Bounds.X, _
Screen.PrimaryScreen.Bounds.Y, _
0, 0, _
Screen.PrimaryScreen.Bounds.Size, _
CopyPixelOperation.SourceCopy)
End Using
'This is where you tell it WHAT PIXEL to check
Dim pixelColor As Color = bm.GetPixel(450, 75)
PictureBox1.BackColor = pixelColor
TextBox1.Text = PictureBox1.BackColor.Name
End Sub
End Class
来源:https://stackoverflow.com/questions/13443913/trying-to-get-pixel-color-from-screen-coordinates-cant-call-my-function