0

我正在尝试获取设置屏幕坐标的像素颜色。我找到了一个函数,由于某种原因我不能调用它。我将它放入一个新类中,我试图从我的主窗体中调用它,尽管它无法识别该函数。它是一个公共类和一个公共函数,所以我不知道为什么。谢谢。

    #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
4

2 回答 2

1

要回答您的问题,您需要创建该类的一个实例:

Dim t As New Test
Dim pc As Color = t.GetPixelColor(20, 20)

或者将该函数设为公共共享函数,那么您将不需要测试实例:

Dim pc As Color = test.GetPixelColor(20, 20)

或者,您可以只截屏并使用内置的bitmap.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
于 2012-11-18T22:31:21.233 回答
1
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
于 2015-01-31T23:29:18.760 回答