I was initially calling GetPixel in a loop to scan a screenshot for a specific RBG which caused a 2-3 delay before finding the color until I realized it's much faster to use LockBits and UnlockBits instead.
Anyway, here's my source for finding a specific RBG color on your screen and then automatically moving the mouse to that color which is good for browser shooting games and what not.
Compiled in vs2010.
Anyway, here's my source for finding a specific RBG color on your screen and then automatically moving the mouse to that color which is good for browser shooting games and what not.
Compiled in vs2010.
Code:
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ColorToFind As Int32 = Color.FromArgb(255, 137, 70, 5).ToArgb 'first # always 255
Dim x As Int32 = -1
Dim y As Int32
Dim bmp As Bitmap = New Bitmap(CaptureToBitmap(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height))
Dim b(bmp.Width * bmp.Height - 1) As Int32
Dim bd As BitmapData = bmp.LockBits(New Rectangle(Point.Empty, bmp.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppRgb)
Marshal.Copy(bd.Scan0, b, 0, b.Length)
bmp.UnlockBits(bd)
'Find color
For i As Int32 = 0 To b.Length - 1
If b(i) = ColorToFind Then
x = i Mod bmp.Width
y = i \ bmp.Width
Cursor.Position = New Point(x, y)
Exit For
End If
Next
' Color was found?
If x = -1 Then
MessageBox.Show("Color Not Found")
End If
bmp.Dispose()
End Sub
Private Function CaptureToBitmap(ByVal X As Int32, ByVal Y As Int32, ByVal W As Int32, ByVal H As Int32) As Bitmap
Dim bmp As New Bitmap(W, H)
Using g = Graphics.FromImage(bmp)
g.CopyFromScreen(New Point(X, Y), Point.Empty, New Size(W, H))
End Using
Return bmp
End Function