如何獲取光標位置的像素顏色?我知道如何使用MousePosition獲取鼠標位置,但我無法弄清楚如何獲取該位置的像素顏色。如何使用VB.NET獲取光標位置處的像素的RGB顏色?
3
A
回答
4
C#版本:How do I get the colour of a pixel at X,Y using c# ?
應該很容易在VB.NET中重寫。
-1
這實際上比你想象的要難。我會尋找一些已經做到的示例代碼,並複製它們的技巧。
最後,該算法將不得不執行這些操作:
- 拿到桌面/窗口的位圖接近光標位置
- 指數與光標位置是位圖和提取像素顏色
聽起來很簡單,但並不容易。
+0
這就是我一直看,但我已經能夠找到的唯一示例代碼是C++,我不明白它的大部分。 – Bryan 2009-11-01 17:39:27
1
快速簡單很慢,但它的作品。
這個想法是將屏幕複製到一個位圖,可以使用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
享受。
1
試試這個代碼:
#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
你需要按鈕(名稱== Button1的)和標籤(名稱== Label1的)。要從屏幕獲取顏色,您需要使用計時器。
如果你需要重寫從C#代碼到VB.NET使用此鏈接:http://www.harding.edu/fmccown/vbnet_csharp_comparison.html
0
保持簡單,不是很優化,但做的工作:
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
相關問題
- 1. 如何獲得光標下的像素顏色?
- 2. 如何獲取鼠標位置的面板像素的顏色(RFC)
- 3. 使用pygame在位置獲取像素顏色
- 4. WriteableImage - 如何獲取像素的顏色?
- 5. 在C++中用聚焦窗口獲取像素RGB顏色
- 6. 在Android模擬器的x,y位置獲取像素顏色
- 7. 如何獲取像素縮放位圖上的顏色值?
- 8. 查找給定RGB顏色的像素
- 9. 試圖獲取光標的顏色
- 10. 使用Magick獲取像素顏色++
- 11. 使用PIL獲取像素的RGB
- 12. 如何從鼠標點擊中拾取一個像素的RGB顏色值
- 13. 如何從像素獲取顏色? OpenGL
- 14. 從鼠標的緩衝圖像中獲取像素RGB X和Y的位置
- 15. 如何將圖像顏色從RGB 24位量化爲RGB 6位(64色)?
- 16. OpenCV:如何填充每個像素的每個RGB顏色值的RGB圖像?
- 17. 背景顏色基礎上的光標位置,特定顏色的光譜
- 18. 如何獲取TextInput中的光標位置和光標位置後的內容?
- 19. 如何獲取圖像中像素的顏色(加載灰色)?
- 20. 如何獲得RGB數值相當於vb.net中的顏色
- 21. FreeImage:獲取像素顏色
- 22. GDAL獲取像素顏色
- 23. Java - 獲取像素顏色
- 24. 獲取輸入元素的光標或文本位置(以像素爲單位)
- 25. 如何獲取和設置CMYK格式的像素顏色?
- 26. ActionScript3.0 - 如何在座標處獲取像素的顏色(uint)? (Stage3D,Flare3D)
- 27. 圖像讀取的RGB顏色?
- 28. 光標在UITextView中的像素位置
- 29. 光滑的利用紋理爲像素獲取RGB值
- 30. 如何使用Applescript獲取圖像的顏色配置文件?
+1有些工具可以將C#轉換爲VB.Net(至少在某種程度上),所以您可能需要將代碼複製到此處並查看所得到的結果:http://www.developerfusion.com/tools/convert/ CSHARP到VB /。 – Groo 2009-11-01 17:53:48