Find pixel with matching color
Hi everyone,
I am trying to make an kinda autoclicker when the desired color is found.
I am using a class to make an screenshot of the screen.
VB Code:
Public Class CaptureScreen
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer
Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer
Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
Const SRCCOPY As Integer = &HCC0020
Public Shared Function GetScreen()
Dim oBackground As Bitmap
Dim FW, FH As Integer
Dim hSDC, hMDC As Integer
Dim hBMP, hBMPOld As Integer
Dim r As Integer
hSDC = CreateDC("DISPLAY", "", "", "")
hMDC = CreateCompatibleDC(hSDC)
FW = GetDeviceCaps(hSDC, 8)
FH = GetDeviceCaps(hSDC, 10)
hBMP = CreateCompatibleBitmap(hSDC, FW, FH)
hBMPOld = SelectObject(hMDC, hBMP)
r = BitBlt(hMDC, 0, 0, FW, FH, hSDC, 0, 0, 13369376)
hBMP = SelectObject(hMDC, hBMPOld)
r = DeleteDC(hSDC)
r = DeleteDC(hMDC)
oBackground = Image.FromHbitmap(New IntPtr(hBMP))
DeleteObject(hBMP)
Return oBackground
End Function
End Class
Credits go to Modderman.
Now I wanna make an functions that find the a pixel with the matching color I put in as RGB in 3 textboxes 1 for R etc.
How do I do this?
Already thanks,
Smurf
Re: Find pixel with matching color
Something like this, but it is probably not a very fast function, as it has to interrogate every pixel in an image to find a match. So if an image is 500x500, that is 250000 pixels it has to interrogate.
Code:
Dim i As Integer
Dim j As Integer
Dim PixelColor As Color
Dim usercolor As Color
usercolor = Color.FromArgb(0, Integer.Parse(red.Text), Integer.Parse(green.Text), Integer.Parse(blue.Text))
For i = 0 To b.Width
For j = 0 To b.Height
PixelColor = b.GetPixel(j, i)
If PixelColor = usercolor Then
'Found usercolor
End If
Next
Next
Re: Find pixel with matching color
Somehow it doesn't work as first it give an error for the color.
And 2nd it gives an error at PixelColor = Spic.GetPixel(j, i)
"The parameter msut be positive and < Height Parameter name: y"
Re: Find pixel with matching color
Try setting the lower bound of the loops to 1 instead of 0.