Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 试图从屏幕坐标中获取像素颜色,无法调用我的函数_.net_Vb.net_Winforms_Visual Studio 2010 - Fatal编程技术网

.net 试图从屏幕坐标中获取像素颜色,无法调用我的函数

.net 试图从屏幕坐标中获取像素颜色,无法调用我的函数,.net,vb.net,winforms,visual-studio-2010,.net,Vb.net,Winforms,Visual Studio 2010,我正在尝试获取设置屏幕坐标的像素颜色。我找到了一个函数,由于某种原因我不能调用它。我把它放在一个新类中,我试图从我的主窗体调用它,尽管它无法识别函数。这是一个公共类和一个公共函数,所以我不知道为什么。谢谢 #Region "#include" Imports System Imports System.Drawing Imports System.Runtime.InteropServices #End Region Public Class Te

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

    #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
#地区“#包括”
导入系统
导入系统。绘图
导入System.Runtime.InteropServices
#末端区域
公开课考试
#区域“来自Windows API”
_
公共共享函数GetWindowDC(ByVal hwnd作为IntPtr)作为IntPtr
'不要尝试将此方法命名为“GetDC”,它会说user32没有GetDC!!!
端函数
_
公共共享函数ReleaseDC(ByVal hwnd作为IntPtr,ByVal hdc作为IntPtr)作为Int32
端函数
_
公共共享函数GetPixel(ByVal hdc作为IntPtr,ByVal nXPos作为Integer,ByVal NPPOS作为Integer)作为UInteger
端函数
#末端区域
REM——测试--
#区域“某些功能”
公共函数GetPixelColor(ByVal x为整数,ByVal y为整数)为颜色
Dim hdc As IntPtr=GetWindowDC(IntPtr.Zero)
将像素变暗为UInteger=GetPixel(hdc,x,y)
暗淡的颜色
释放DC(IntPtr.Zero,hdc)
MsgBox(像素)
颜色=颜色。来自argb(Int(像素和&HFF)_
Int(像素和&HFF00)>>8_
整数(像素和&HFF0000)>>16)
返回颜色
端函数
#末端区域
末级

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

Dim t As New Test
Dim pc As Color = t.GetPixelColor(20, 20)
或者将该函数设置为公共共享函数,则不需要测试实例:

Dim pc As Color = test.GetPixelColor(20, 20)
或者,您也可以只截图,使用内置类,同时避免使用该类:

   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

谢谢你的回复,我试着让它成为一个公共共享功能,但它仍然无法识别它。它也是一个公共类。我刚刚在vs2010中测试了它,它的效果很好。创建类实例并尝试使用该实例时会发生什么情况?为窗体窗口应用程序打开一个新项目(如果使用visual studio),添加1个文本框(保留名称作为默认名称,或者根据需要命名,但确保编辑上面的代码以反映名称)添加1个picturebox(同样,和以前一样,保留默认名称,或者更改它,但也要记住编辑代码)就是这样!-我已经注释掉了一个参数,因此您可以选择是否希望位图渲染整个屏幕,然后从中获取特定像素,或者(如果这太需要处理器)让它保持如上图所示的状态—裁剪屏幕的一部分,然后将其保存到位图变量—更不用说渲染了,而且性能显著提高。我创建了它,并通过一个带有win XP的虚拟机运行它,该虚拟机具有1个内核、2.4ghz、1GB虚拟RAM,在后台运行visual studio 2010,(也就是说,项目仍然处于打开状态并运行程序,而不仅仅是保存/导出的.exe文件,这就不需要考虑了)我将时间间隔设置为每毫秒刷新/滴答一次,任务管理器的使用率几乎不超过4-6%。当我在全屏上做同样的测试时,它仍然只有10-12%左右,但如果你移动鼠标或打开其他程序等,CPU的使用率将跃升到100%。我希望上面的代码能帮助人们——你没有想一想得到那个代码——特别是那个代码,你从屏幕x y坐标得到一个像素,而不是从鼠标光标得到一个像素——这是一场多么可怕的噩梦,我发现了非常接近的匹配,但它们比一个性欲旺盛的青少年更能吸引你的CPU。上面的代码允许你拍摄到某个屏幕点或整个屏幕,直到无论您希望它如何定期刷新,都会不断刷新-我对计时器inverval值的位置进行了注释
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