注册 登录
编程论坛 VB.NET论坛

API getpixel 的用法问题

dafeixia88 发布于 2010-02-25 10:02, 2347 次点击
各位精英,小弟最近用API中的getpixel 想获取屏幕上某一点像素,结果是据点有结果全-1,心情十分沉重,万望知道的指点一二,十分感谢,原代码如下:
 Private Declare Function getpixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Long, ByVal x As Integer, ByVal y As Integer) As Integer
 Public Shared Function gaincolor(ByVal p As Point, ByVal aaa As IntPtr) As Long

        'Dim bbb As Color
        Dim ccc As Long
        Dim ddd As Long
        ddd = GetWindowDC(aaa)
        ccc = getpixel(ddd, p.X, p.Y),此处ccc总是返回-1   
        SetPixel(ddd, p.X, p.Y, 300),此处好像没有执行结果
        Return ccc
    End Function

上述函数返回值总是-1
4 回复
#2
dafeixia882010-02-25 16:47
Public Shared Function gaincolor(ByVal p As Point, ByVal aaa As IntPtr) As Int32
        Dim ccc As IntPtr
        Dim ddd As Int32
        ccc = GetDC(0)
        ddd = getpixel(ccc, 200, 100)
        ReleaseDC(0, ccc)
        Return ddd
    End Function
改进代码,还是不对,每次获得数值不一样
#3
dafeixia882010-02-25 17:46
有人能给我一个最简单的例子吗?比如取屏幕上200,200像素处点的颜色
我用的VB2005,谢谢!!!
#4
dafeixia882010-02-26 20:57
算了,不用它了,改用截屏取色吧,已经完成,谢谢大家
#5
a4134591642010-03-19 21:16
又不用那么灰心,截屏太麻烦了,,,

Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Intptr, ByVal x As Integer, ByVal y As Integer) As Interger  '注意 .net 中句柄类型都改成                                                        Intptr      /.............  
 
  Public struce POINTAPI  '这个改改,(struce)
        x As Long
        y As Long
End Type
Public Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (Byref lpPoint As POINTAPI) As bool   '注意这里的byref一定要传址...

Declare Function GetDC Lib "user32" Alias "GetDC" (ByVal hwnd As intptr) As intptr
dim p as new POINTAPI
getCursorPos(p)

GetPixel(GetDC(Intptr.zero),p.x,p.y)

   我是用C#的,这里的代码可能有些出入,自己改改
1