Results 1 to 4 of 4

Thread: Find pixel with matching color

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    44

    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:
    1. Public Class CaptureScreen
    2.     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
    3.  
    4.     Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    5.  
    6.     Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer
    7.  
    8.     Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer
    9.  
    10.     Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer
    11.  
    12.     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
    13.  
    14.     Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer
    15.  
    16.     Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer
    17.  
    18.     Const SRCCOPY As Integer = &HCC0020
    19.  
    20.  
    21.  
    22.  
    23.     Public Shared Function GetScreen()
    24.         Dim oBackground As Bitmap
    25.         Dim FW, FH As Integer
    26.         Dim hSDC, hMDC As Integer
    27.         Dim hBMP, hBMPOld As Integer
    28.         Dim r As Integer
    29.  
    30.         hSDC = CreateDC("DISPLAY", "", "", "")
    31.         hMDC = CreateCompatibleDC(hSDC)
    32.  
    33.         FW = GetDeviceCaps(hSDC, 8)
    34.         FH = GetDeviceCaps(hSDC, 10)
    35.  
    36.         hBMP = CreateCompatibleBitmap(hSDC, FW, FH)
    37.  
    38.         hBMPOld = SelectObject(hMDC, hBMP)
    39.         r = BitBlt(hMDC, 0, 0, FW, FH, hSDC, 0, 0, 13369376)
    40.         hBMP = SelectObject(hMDC, hBMPOld)
    41.  
    42.         r = DeleteDC(hSDC)
    43.         r = DeleteDC(hMDC)
    44.  
    45.         oBackground = Image.FromHbitmap(New IntPtr(hBMP))
    46.         DeleteObject(hBMP)
    47.         Return oBackground
    48.     End Function
    49.  
    50. 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

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    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

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2007
    Posts
    44

    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"

  4. #4
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: Find pixel with matching color

    Try setting the lower bound of the loops to 1 instead of 0.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width