Help With Pixel Scanning

Posts 1–8 of 8 · Page 1 of 1
Help With Pixel Scanning
Okay, so basically i need to find a color and click on that color. Im very new to VB and i would appreciate any help with this.
I'll give you the code if you be a little more specific, but not going to personally teach you the basics of programming. Do you have a specific question, or. ?
Quote Originally Posted by abuckau907 View Post
I'll give you the code if you be a little more specific, but not going to personally teach you the basics of programming. Do you have a specific question, or. ?
So I need it to wait for a certain color to appear and then click it once it does appear
take screenshot. will put the code together for you in next couple hours. Really isn't a whole lot to it, < 50 lines probably.
Quote Originally Posted by abuckau907 View Post
take screenshot. will put the code together for you in next couple hours. Really isn't a whole lot to it, < 50 lines probably.
Sounds great man. I appreciate your help!

Crap. Went over 50 lines, but...
 
Form1

Public Class Form1

Private _targetColor As Color '' yay for global variables
Private _screenShot As Bitmap = New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height) '' no dual monitor support here
Private _forceStop As Boolean = False '' thread loop control
Private _isRunning As Boolean = False '' thread loop control
Private _wThread As Threading.Thread '' background worker thread

Private Sub cmdSetColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetColor.Click
_targetColor = modPublic.GetPixelColor(Cursor.Position.X, Cursor.Position.Y)
Panel1.BackColor = _targetColor ''update gui so user can see the color
lblScanColorRGB.Text = "rgb " & _targetColor.R & ", " & _targetColor.G & ", " & _targetColor.B '' should call .ToString() on them : too long/verbose.
End Sub

Private Sub cmdOnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOnOff.Click
If cmdOnOff.Text = "Turn On" Then
If _isRunning Then
MsgBox("Thread is still busy, please wait a moment for the thread to end and try again.")
Else
_forceStop = False '' sanity check
_isRunning = True
_wThread = New Threading.Thread(AddressOf BotOn)
_wThread.Start()
cmdOnOff.Text = "Turn Off"
End If
Else
_forceStop = True
cmdOnOff.Text = "Turn On"
End If
End Sub

Private Sub BotOn()
Do Until _forceStop = True
_screenShot = modPublic.GetScreenShot()
''loop over each pixel (left to right, top to bottom) and check for target color
Dim _tmpPixelColor As Color
For xx As Int32 = 1 To (_screenShot.Width - 1)
For yy As Int32 = 1 To (_screenShot.Height - 1)
_tmpPixelColor = _screenShot.GetPixel(xx, yy)
If _tmpPixelColor.R = _targetColor.R And _tmpPixelColor.G = _targetColor.G And _tmpPixelColor.B = _targetColor.B Then
''the pixel matches our color. Success.
Cursor.Position = New Point(xx, yy) ''move cursor to the color's location
Threading.Thread.Sleep(500) '' wait a moment because the game may store the cursor's position and it needs to be refreshed.
If chkBeep.Checked Then
Beep() '' single small beeping noise from pc's on-board speaker. why not.
End If

modPublic.LeftClick()
Threading.Thread.Sleep(CInt(numSleepTime.Value * 1000))
Continue Do
End If
Next
If _forceStop Then Exit For '' 1 line IF statment. awk but doesn't deserve 3 lines..
Next
Loop
_forceStop = False '' we left then loop because it was true, now set it back to false.
_isRunning = False
End Sub

Private Sub chkHideColorBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHideColorBox.CheckedChanged
Panel1.Visible = Not chkHideColorBox.Checked ' aka below
'If chkHideColorBox.Checked Then
'Panel1.Visible = False
'Else
'Panel1.Visible = True
'End If
End Sub


End Class


+
 
modPublic

Module modPublic
''Windows API
Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
Private Declare Function mouse_event Lib "user32" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer) As Boolean

Private Const mouse_leftDown As Integer = 2
Private Const mouse_leftUp As Integer = 4

Public Sub LeftClick()
mouse_event(mouse_leftDown, 0, 0, 0, 0) '' left click down
Threading.Thread.Sleep(250)
mouse_event(mouse_leftUp, 0, 0, 0, 0) '' left click release
End Sub
Public Function GetPixelColor(ByVal x As Int32, ByVal y As Int32) As Color
Dim hWnd As IntPtr = GetDesktopWindow()
Dim hDC As IntPtr = GetWindowDC(hWnd)
Dim lColor As Int32 = GetPixel(hDC, x, y)
ReleaseDC(hWnd, hDC)
Return System.Drawing.ColorTranslator.FromWin32(lColor) '' color struct is like 20+ bytes :/
End Function
Public Function GetScreenShot() As Bitmap
Dim _rtnBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
Dim _gr As Graphics = Graphics.FromImage(_rtnBmp)
_gr.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
_gr.Dispose()
Return _rtnBmp
End Function
End Module


Add all the controls (Labels, Buttons, checkbox) and re-name them. Should work copy/paste style. Or I'll up upload the project file somewhere for you. You should be able to get it to work. : p Tested it several times, seems to be working as expected.

edit: I uploaded the project files (and .exe!) Pm'd you the link.
wwwdotsend spacedotcom/file/ o 2 2 z z 1
Quote Originally Posted by abuckau907 View Post

Crap. Went over 50 lines, but...
 
Form1

Public Class Form1

Private _targetColor As Color '' yay for global variables
Private _screenShot As Bitmap = New Bitmap(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height) '' no dual monitor support here
Private _forceStop As Boolean = False '' thread loop control
Private _isRunning As Boolean = False '' thread loop control
Private _wThread As Threading.Thread '' background worker thread

Private Sub cmdSetColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetColor.Click
_targetColor = modPublic.GetPixelColor(Cursor.Position.X, Cursor.Position.Y)
Panel1.BackColor = _targetColor ''update gui so user can see the color
lblScanColorRGB.Text = "rgb " & _targetColor.R & ", " & _targetColor.G & ", " & _targetColor.B '' should call .ToString() on them : too long/verbose.
End Sub

Private Sub cmdOnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOnOff.Click
If cmdOnOff.Text = "Turn On" Then
If _isRunning Then
MsgBox("Thread is still busy, please wait a moment for the thread to end and try again.")
Else
_forceStop = False '' sanity check
_isRunning = True
_wThread = New Threading.Thread(AddressOf BotOn)
_wThread.Start()
cmdOnOff.Text = "Turn Off"
End If
Else
_forceStop = True
cmdOnOff.Text = "Turn On"
End If
End Sub

Private Sub BotOn()
Do Until _forceStop = True
_screenShot = modPublic.GetScreenShot()
''loop over each pixel (left to right, top to bottom) and check for target color
Dim _tmpPixelColor As Color
For xx As Int32 = 1 To (_screenShot.Width - 1)
For yy As Int32 = 1 To (_screenShot.Height - 1)
_tmpPixelColor = _screenShot.GetPixel(xx, yy)
If _tmpPixelColor.R = _targetColor.R And _tmpPixelColor.G = _targetColor.G And _tmpPixelColor.B = _targetColor.B Then
''the pixel matches our color. Success.
Cursor.Position = New Point(xx, yy) ''move cursor to the color's location
Threading.Thread.Sleep(500) '' wait a moment because the game may store the cursor's position and it needs to be refreshed.
If chkBeep.Checked Then
Beep() '' single small beeping noise from pc's on-board speaker. why not.
End If

modPublic.LeftClick()
Threading.Thread.Sleep(CInt(numSleepTime.Value * 1000))
Continue Do
End If
Next
If _forceStop Then Exit For '' 1 line IF statment. awk but doesn't deserve 3 lines..
Next
Loop
_forceStop = False '' we left then loop because it was true, now set it back to false.
_isRunning = False
End Sub

Private Sub chkHideColorBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHideColorBox.CheckedChanged
Panel1.Visible = Not chkHideColorBox.Checked ' aka below
'If chkHideColorBox.Checked Then
'Panel1.Visible = False
'Else
'Panel1.Visible = True
'End If
End Sub


End Class


+
 
modPublic

Module modPublic
''Windows API
Private Declare Function GetDesktopWindow Lib "user32" () As IntPtr
Private Declare Function GetPixel Lib "gdi32" (ByVal hDC As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function GetWindowDC Lib "user32" (ByVal hWnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
Private Declare Function mouse_event Lib "user32" (ByVal dwflags As Integer, ByVal dx As Integer, ByVal dy As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer) As Boolean

Private Const mouse_leftDown As Integer = 2
Private Const mouse_leftUp As Integer = 4

Public Sub LeftClick()
mouse_event(mouse_leftDown, 0, 0, 0, 0) '' left click down
Threading.Thread.Sleep(250)
mouse_event(mouse_leftUp, 0, 0, 0, 0) '' left click release
End Sub
Public Function GetPixelColor(ByVal x As Int32, ByVal y As Int32) As Color
Dim hWnd As IntPtr = GetDesktopWindow()
Dim hDC As IntPtr = GetWindowDC(hWnd)
Dim lColor As Int32 = GetPixel(hDC, x, y)
ReleaseDC(hWnd, hDC)
Return System.Drawing.ColorTranslator.FromWin32(lColor) '' color struct is like 20+ bytes :/
End Function
Public Function GetScreenShot() As Bitmap
Dim _rtnBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb)
Dim _gr As Graphics = Graphics.FromImage(_rtnBmp)
_gr.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
_gr.Dispose()
Return _rtnBmp
End Function
End Module


Add all the controls (Labels, Buttons, checkbox) and re-name them. Should work copy/paste style. Or I'll up upload the project file somewhere for you. You should be able to get it to work. : p Tested it several times, seems to be working as expected.

edit: I uploaded the project files (and .exe!) Pm'd you the link.
wwwdotsend spacedotcom/file/ o 2 2 z z 1
Wow thanks so much! I'll test it when I get a chance
yep yep. one thing that made me laugh was after i set the color and started, because the color is shows in a big box on the form, it found the color on itself instead of on the desktop like it was supposed to. Anyway, let me know. Tested on win 7. You need .net framework..4 I believe. Project files are for msvs 2010.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us