2009-11-01 32 views

回答

4

C#版本:How do I get the colour of a pixel at X,Y using c# ?
應該很容易在VB.NET中重寫。

+1

+1有些工具可以將C#轉換爲VB.Net(至少在某種程度上),所以您可能需要將代碼複製到此處並查看所得到的結果:http://www.developerfusion.com/tools/convert/ CSHARP到VB /。 – Groo 2009-11-01 17:53:48

-1

這實際上比你想象的要難。我會尋找一些已經做到的示例代碼,並複製它們的技巧。

最後,該算法將不得不執行這些操作:

  1. 拿到桌面/窗口的位圖接近光標位置
  2. 指數與光標位置是位圖和提取像素顏色

聽起來很簡單,但並不容易。

+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 
相關問題