如何使用VB.NET获取光标位置处像素的RGB颜色?
如何获取光标位置的像素颜色?我知道如何使用MousePosition获得鼠标位置,但我不知道如何获得该位置的像素颜色。这实际上比你想象的要难。我会寻找一些已经做到这一点的示例代码,并复制他们的技术 最后,算法必须执行以下操作:如何使用VB.NET获取光标位置处像素的RGB颜色?,vb.net,Vb.net,如何获取光标位置的像素颜色?我知道如何使用MousePosition获得鼠标位置,但我不知道如何获得该位置的像素颜色。这实际上比你想象的要难。我会寻找一些已经做到这一点的示例代码,并复制他们的技术 最后,算法必须执行以下操作: 获取靠近光标位置的桌面/窗口位图 用光标位置索引到位图中并提取像素颜色 这听起来很简单,但并不容易。C版本: 应该很容易在VB.NET中重写。快速简单非常慢,但它可以工作 这个想法是将屏幕复制到位图,可以使用GDI+内置到drawing.graphics对象中来完成。然后
应该很容易在VB.NET中重写。快速简单非常慢,但它可以工作 这个想法是将屏幕复制到位图,可以使用GDI+内置到drawing.graphics对象中来完成。然后简单地读取它生成的位图。获取像素的速度非常慢。最好的方法是直接读取图像字节数组
Function MakeScreenShot() As Drawing.Bitmap
Dim out As Drawing.Bitmap
'Get the screen Size
Dim bounds As Rectangle = Screen.GetBounds(Point.Empty)
'create the bitmap
out = New Drawing.Bitmap(bounds.Width, bounds.Height)
'create a graphic object to recive the pic
Using gr As Drawing.Graphics = Graphics.FromImage(out)
'Copy the screen using built in API
gr.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size)
End Using
Return out
End Function
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim BM As Drawing.Bitmap = MakeScreenShot()
Dim mouseloc As Point = Cursor.Position
Dim c As Color = BM.GetPixel(mouseloc.X, mouseloc.Y) ' The Slowest way possable to read a color
Debug.Print(c.R & "," & c.G & "," & c.B)
End Sub
享受。尝试以下代码:
#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
REM --Test--
#Region "OnClick"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim mouseloc As Point = Cursor.Position
Me.Label1.BackColor = GetPixelColor(mouseloc.X, mouseloc.Y)
Me.Refresh()
End Sub
#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)
返回颜色
端函数
#末端区域
REM——测试--
#区域“OnClick”
私有子按钮1\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮1。单击
点=光标位置时的鼠标光标尺寸
Me.Label1.BackColor=GetPixelColor(mouseloc.X,mouseloc.Y)
我
端接头
#末端区域
末级
您需要按钮(name==Button1)和标签(name==Label1)。要从屏幕上获取颜色,您需要使用计时器
如果您需要将代码从C#重写到VB.NET,请使用以下链接:保持简单,不要进行太多优化,但要完成以下工作:
Public Class Form1
Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function GetDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Declare Function ReleaseDC Lib "user32" (ByVal hwnd As IntPtr, ByVal hdc As IntPtr) As IntPtr
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Int32, ByVal Y As Int32) As Int32
Private Function CheckScreen()
Dim CursorLoc As Point = Cursor.Position
Dim hDC As IntPtr = GetDC(0) '0 = Get the color to any window on screen, not only your app
Try
Dim CursorColor As Integer = GetPixel(hDC, CursorLoc.X, CursorLoc.Y)
Me.Panel1.BackColor = ColorTranslator.FromWin32(CursorColor) 'a simple panel to show the color
Me.Refresh()
Return True
Catch ex As Exception
Return False
Finally
ReleaseDC(0, hDC)
End Try
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Timer1.Start() 'Timer with a short delay
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
CheckScreen()
End Sub
End Class
这就是我所看到的,但我唯一能找到的代码是C++,而且我不太理解它。1有一些工具将C.*转换成VB.NET(至少在某种程度上),所以你可能想复制代码,看看你得到了什么。