>我正在尝试获取设置屏幕坐标的像素颜色。我找到了一个函数,但由于某种原因我无法调用它。我把它放到一个新类中,我试图从我的主窗体调用它,尽管它不识别函数。这是一个公共类和一个公共功能,所以我不确定为什么。谢谢。
#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
要回答您的问题,您需要创建该类的实例:
Dim t As New Test
Dim pc As Color = t.GetPixelColor(20, 20)
或者将该函数设置为公共共享函数,那么您就不需要测试实例:
Dim pc As Color = test.GetPixelColor(20, 20)
或者,您可以截屏并使用内置的位图。GetPixel并一起避免该类:
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
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