0

設定された画面座標のピクセル色を取得しようとしています。見つけた関数がありますが、何らかの理由で呼び出すことができません。私はそれを新しいクラスに配置し、関数を認識していませんが、メインフォームから呼び出そうとしています。これは public クラスと public 関数なので、理由はわかりません。ありがとう。

    #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
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 に答える