[SOURCE] Simple Find Pixel Color [/SOURCE]

Discussion in 'Programming General' started by Darthatron, Nov 6, 2007.

[SOURCE] Simple Find Pixel Color [/SOURCE]
  1. Unread #1 - Nov 6, 2007 at 5:25 PM
  2. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    Alright, this is a VERY SIMPLE find pixel color function. It is slow and shiet, but hey, whatever floats your boat.

    Add this to General Declarations...
    Code:
    Option Explicit
    
    Private Type PointApi
        X As Long
        Y As Long
    End Type
    
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As PointApi) As Long
    Private Declare Function SetCursorPos Lib "user32.dll" (ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
            
    Dim PosXY As PointApi
    Dim Retval As Long
    Dim tPosXY As PointApi
    Dim sTemp As String
    Dim lColor As Long
    Dim lDC As Long
    Dim iX, iY As Integer
    Dim ColorPixel As Long
    To actually get the pixel you want to look for use this code... Put it in an Image_MouseUp or something.
    Code:
        lDC = GetWindowDC(0)
        Call GetCursorPos(tPosXY)
        lColor = GetPixel(lDC, tPosXY.X, tPosXY.Y)
        Picture1.BackColor = lColor
    This is the Function that will search the screen for that Pixel Color...
    Code:
    Function FindPixelColor(Color As Long)
        If Color > -1 And Color < 16777216 Then 'Checks if the Color is a Color or not.
            For iY = 0 To Screen.Height 'Gets the screens height.
                For iX = 0 To Screen.Width 'Gets the screens width.
                    ColorPixel = GetPixel(lDC, iX, iY) 'Gets the pixel color at location.
                    If Color = ColorPixel Then 'Checks if the colors match.
                            SetCursorPos Val(iX), Val(iY) 'Change this to a MoveMouseSmooth function, or whatever...
                        Exit Function 'Exits the Function.
                    End If
                Next iX 'Tries the next X location.
            Next iY 'Starts again at the next Y location.
            MsgBox "The color was not found.", vbOKOnly, "Error."
        Else
            MsgBox "That color is invalid.", vbOKOnly, "Error."
        End If
    End Function
    You could edit that code to return the values of the X and Y values if you know how. Use it well.
     
  3. Unread #2 - Nov 7, 2007 at 11:36 AM
  4. Terrankiller
    Joined:
    May 7, 2005
    Posts:
    1,286
    Referrals:
    1
    Sythe Gold:
    1

    Terrankiller Ex-Administrator
    Retired Administrator Visual Basic Programmers

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    There are so many things you can do to make that code cleaner and run faster.
     
  5. Unread #3 - Nov 7, 2007 at 4:34 PM
  6. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    Ok, here's what you need to do to fix that. Use LONG TYPE variables for your For loops! It's that simple.

    Declare them both as LONG variables and say goodbye to slow and shitty!:D
    Code:
    Dim iX As Long, iY As Long
    
     
  7. Unread #4 - Nov 8, 2007 at 12:49 AM
  8. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    I know its bad guys, but I was tired and bored. (Sleep is for the weak.)
     
  9. Unread #5 - Nov 8, 2007 at 12:52 AM
  10. TroubleSleep
    Joined:
    Nov 7, 2007
    Posts:
    160
    Referrals:
    0
    Sythe Gold:
    0

    TroubleSleep Active Member

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    Sausage.
     
  11. Unread #6 - Nov 8, 2007 at 7:04 AM
  12. Covey
    Joined:
    Sep 9, 2005
    Posts:
    4,510
    Referrals:
    9
    Sythe Gold:
    9
    Discord Unique ID:
    807246764155338833
    Discord Username:
    Covey#1816

    Covey Creator of EliteSwitch
    Retired Sectional Moderator Visual Basic Programmers

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    The only way i can see to make it faster is to manipulate the search area.
    Other then that i'd say good job.

    EDIT:
    Fuck off, seriously.
    Take your shit elsewhere.
     
  13. Unread #7 - Nov 8, 2007 at 9:28 AM
  14. Jukka
    Joined:
    Nov 23, 2005
    Posts:
    244
    Referrals:
    0
    Sythe Gold:
    0

    Jukka Active Member

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    if u want to make findcolorTolerance i could help ..

    Basically its just math counting the color (example: color 255)

    Dim Red As Extended

    Red = Color / 255;
    Green = Color / 255 / 255;
    Blue = Color / 255 / 255;

    Im not entirely sure about this but.. Something like that..

    How to check tolerance?
    Basically its just some math

    if u didnt understand i can give u some code..
     
  15. Unread #8 - Nov 8, 2007 at 8:26 PM
  16. Darthatron
    Joined:
    May 22, 2006
    Posts:
    1,612
    Referrals:
    3
    Sythe Gold:
    0

    Darthatron Massive Troll
    Retired Sectional Moderator Visual Basic Programmers

    [SOURCE] Simple Find Pixel Color [/SOURCE]

    Thanks Jukka, but I already know how do Color Tolerance. =)

    @TroubleSleep: Dude... really... don't spam... GTFO.
     
< Hiscores Lookup? | SMRDll - Autoing Functions - BETA >

Users viewing this thread
1 guest


 
 
Adblock breaks this site