Results 1 to 12 of 12

Thread: GetPixel...Different Issue

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Resolved GetPixel...Different Issue

    I have read through at least 20 GetPixel API threads on this forum as well as other places on the internet and I must say, I have made no progress. I started out writing macros for games mainly relying on color checks, loading the RGB value of a specific pixel in a game, namely World of Warcraft, and then comparing it.

    I can't seem to find out however how to use the VB6 API GetPixel. Basically, I am trying to load a pixel, lets say, 500, 400. Then I want to ask, is it the RED Value = 100. If not, delay, or whatever. I know how to do all of this and I am not for one second asking for anyone to write me a macro as I find learning Visual Basic is fun, I am just extremely confused as to how I go about doing this whole color check buisness.

    Thank you very much for your reply and please don't link me to the example vault as I have studied and tried to understand it many times already. Thank you!!!

    -Wizzel
    Last edited by Wizzel; Jan 5th, 2007 at 02:22 AM.

  2. #2
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    Jar, Norway
    Posts
    372

    Re: GetPixel...Different Issue

    Is this what you whish to do? http://webexhibits.org/feast/pigments/index.html

  3. #3
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: GetPixel...Different Issue

    RGBx values are all stored together stored as a single Long and you need to do some tinkering to get each color..
    VB Code:
    1. r = lColor And &HFF
    2.         g = lColor \ &H100 And &HFF
    3.         b = lColor \ &H10000 And &HFF
    For your situation, GetPixel() should return its value in lColor format

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: GetPixel...Different Issue

    Thanks for the replies guys. I am still a bit confused. Specifically, for example, I want the program to lets say if the pixel at 355, 400 has an RGB value of:

    R:250
    G:255
    B:10

    I want the program to respond to that by doing a certain action. I used another scripting language, not VB, but I am still trying to convert the action. The example above of the program is not quite what i want but still encompasses the same ideas where it gathers the RGB values from the image. I don't want simply an image, I want it to gather the value of a single pixel from the entire screen.

    Thank you so much for your replies. I would post my attempts but they don't work at all. Thank you.

    -Wizzel

  5. #5
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: GetPixel...Different Issue

    if getpixel(355,400) = &h0AFFFA then dostuff
    Should do what you need.. Remember rgb's are stored in a single Long that you can think of as BBGGRR if written in hex.. It seems more confusing in decimal since 255 doesn't work nicely with base 10 math, but if you look at vb's big number for 255,255,255.. You'll notice it actually equals 255*255*255 just like &HFFFFFF actually equals FF*FF*FF, and FF=255 in Hexadecimal btw..

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: GetPixel...Different Issue

    Quote Originally Posted by triggernum5
    if getpixel(355,400) = &h0AFFFA then dostuff
    Should do what you need.. Remember rgb's are stored in a single Long that you can think of as BBGGRR if written in hex.. It seems more confusing in decimal since 255 doesn't work nicely with base 10 math, but if you look at vb's big number for 255,255,255.. You'll notice it actually equals 255*255*255 just like &HFFFFFF actually equals FF*FF*FF, and FF=255 in Hexadecimal btw..
    Thanks a bunch for the example. What is an easy way to convert numbers to hexadecimal format? Thanks again.

  7. #7
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: GetPixel...Different Issue

    hex(decimalnumber)
    you can plug it into most code directly..
    eg: r=hex(255) result is the same as r=&HFF.. r=255 also has the same result.. aside from type issues that might exist, they are basically interchangeable..

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Post Re: GetPixel...Different Issue

    Awesome...I will try this out tonight and tell you how it goes!

  9. #9

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: GetPixel...Different Issue

    Okay, I have tried to understand the whole of this and I have done my best to work up a small bit of code. Just to be a beginner and simple, the program will load, check to see if the pixel has the red value specified, 150, and then if not, it closes. I am pretty sure the flow is fine but the code is not. Any advice and or help would be amazing...thank you.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    4. Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
    5.  
    6. Sub Form_Load()
    7.   If GetPixel(355, 400) = (red=150)
    8.     Call Start
    9. Else
    10.   Unload Me
    11. End If
    12.  
    13. Sub Start()
    14.     Sleep 1000
    15. End Sub

    The problem, or at least the main one is I still don't completely grasp the hex values reflecting certain RGB values. If you can fix my code, could you possibly include just examples of looking for the Green value of 50 or a blue value of 95 or anything, and such, it would give me a better understanding. Also, could I include more checks in 1 operation. If G=10 and B=200, then do this. Thanks again for bearing with me as this is the last step in finishing what I wish to accomplish.

    -Alyssa

  10. #10
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: GetPixel...Different Issue

    You still seem to miss the concept of all 3 colors being stored in one value that you have to extract each from.. Imagine that there are only 10 possible values for each red, green, blue.. That means that there are 100 combinations of any 2 of the colors, and 1000 combos of all three.. Then we would use the same method to write color=999 for white (each digit actually represents either r,g,b r=9 g=9 b=9) Hexadecimal colors actually use 2 digits per color component so in our example it would be more like color=999999 giving 100 shades of each color.. Working with 9's is easier for humans, but computers like hexadecimal since FF is a nice round 11111111 (F=1111 btw) so each hex digit nicely transforms into binary..
    So with what I posted earlier:
    If GetPixel(355, 400) = (red=150) should be
    If GetPixel(355, 400) And &HFF = 150 'For Red
    If (GetPixel(355, 400))\&h100 And&hFF = 150 'For Green
    If (GetPixel(355, 400))\&h10000 And&hFF = 150 'For Blue

    Btw, all these maths are doing is knocking unwanted digits off with the divisions and comparing the remainders with the AND operations..
    eg
    467 \ 100 = 4 '(backslash gives integer division ignoring decimal places that forward slash does not ignore)
    467 mod 100 = 67 ' (Mod and AND can be used in similar fashions oftentimes..)
    Try to get a grip with the fact that you are using the signifigance of certain digits in a single value for each color, and a basic hexadecimal/binary/decimal conversion tutorial will save you alot of headache if I didn't explain it adequately..

  11. #11
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: GetPixel...Different Issue

    Quote Originally Posted by Wizzel
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    4. Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
    5.  
    6. Sub Form_Load()
    7.   If GetPixel(355, 400) = (red=150)
    8.     Call Start
    9. Else
    10.   Unload Me
    11. End If
    12.  
    13. Sub Start()
    14.     Sleep 1000
    15. End Sub
    The GetPixel function requires an hDC (Device Context Handle) to the window you're getting the pixel from.

    You'll need to find the Window handle (hWnd) of the game. Possibly using the FindWindow() API function. This may or may not work depending on the game.

    Then you'll need to get the hDC to that window by using the GetDC function. You provide the window handle (hWnd) to this function.

    From there you can use the GetPixel API function with that hDC.

    And then convert it to RGB using triggers code.


    Edit: Logically, it would look something like this:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    5. Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    6.  
    7. Private Sub Form_Load()
    8.     Dim lonHWND As Long 'Handle to game window.
    9.     Dim lonDC As Long 'Device context handle of game window.
    10.     Dim lonColor As Long 'Color value in long format.
    11.    
    12.     'You'll need to get the class name of the window.
    13.     lonHWND = FindWindow("ClassName", "Caption Of Game Window")
    14.    
    15.     If lonHWND <> 0 Then 'Found the window?
    16.         lonDC = GetDC(lonHWND) 'Get device context handle.
    17.        
    18.         If lonDC <> 0 Then 'Got the DC?
    19.             lonColor = GetPixel(lonDC, 350, 400)
    20.            
    21.             'Now convert to RGB.
    22.         End If
    23.    
    24.     End If
    25.    
    26. End Sub
    Last edited by DigiRev; Jan 5th, 2007 at 01:29 PM.

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Posts
    11

    Re: GetPixel...Different Issue

    Thank you both for your replies. I will rewrite the code, do some more research and try again. Thank you again.

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