Link to home
Start Free TrialLog in
Avatar of Jack_Jones
Jack_Jones

asked on

Pixel Detection If True Then, Else , End If

I am looking for some help with understanding how Pixel detection can work in Visual Basic. I am looking for some code help, I want to detect some pixel's on the screen to check if they are true or not. My goal is to make a program that will use google voice, text me when a slot opens up on training that I am doing in a game. This helps me keep an idea on the trainings completed, and what new once I will have to add. I use visual basic 2010 version with Visual Studio. I have this working in AutoIt, but just want to get it working in vb, so I can cut down on how many program im using current to do this, and have just 1 program that can handle it.

Wish it were as simple as:

Private Sub PixelCheck()
'coord to check (x,y)
     if pixel color = FFFFF9 then
              'run this code
     Else
              'the value was false we need to do this instead
     End if
End Sub

Open in new window

You can get the RGB value of a given pixel with something like this:

Dim c As Color = myBitmap.GetPixel(pos.x, pos.y)
Avatar of Jack_Jones
Jack_Jones

ASKER

TommyBoy, how could I utilize that in an if statment like my wish it were a simple as this code lol.
CodeCruser,

 The first example looks really cool, only thing is I don't seen an autoredraw value for the picturebox1 in visual basic 2010. Any suggestions on that?
I should note, I am already able to get the pixelcolor in hex code, not sure if that helps.
Where is autoredraw? What happens if you comment that out?
I have the hex color of the pixel, and the x,y coords already. My hope is to check the Hexcolor @ x,y location and if true then perform an action else do this type of thing.
I'm not quite understanding what you are trying to do. Are you getting the pixel color from a particular position on the screen of a web page (problematic), or from the css value of a particular html element from the stylesheet (do-able), or from an image that's loaded into say a panel your vb program (most accurate)?

When you say "I have the hex color of the pixel" do you mean you have the color of the the pixel you want to compare against an unknown pixel or do you mean I have already captured the unknown pixel and know it's hex value and I just need a way to compare the two pixels?
I would say the statment:

" I have already captured the unknon pixel and know it's hex value and I just need a way to compare the two pixels. "

This would give me a true / false output to work with. the IF / Else Statement. So I guess take the unknown at x,y, and compare it to the known one for a true / false value.
So...you have already captured the unknon pixel and know it's hex value?

And my first question?
I only have the value for the know hexcolor pixel location, what I need to do is find if the value in the (x,y) is true / false.

Say for example it's a white dot, and that white dot changes to blue. What I want is to detect that the white dot, changed to blue and if it did then execute this code or if it's false do this code instead.
>>What I want is to detect that the white dot...

On a web page then? If so, I think you would first have to find the white/blue area on the page. A lot of factors can affect the pixel location, resolution, scroll position, zoom level, window size and position. I assuming you want this to happen programatically while you are not present to "pick" the pixel out with your cursor.
Yeah, thats why I was hopeing to use and IF / ELSE.  But you deff understand what im trying to say / do now ;) lol.

The if/else is the least of your troubles. First you've got to figure out how you are going to get the color of that pixel. I'm not sure how you could accurately pin-point the position on rendered page or from the html. You might be better off pulling it from the css of the page if it's a background color fill and not an image.  Actually, it may be easier to find out just what is different on the page with the blue pixel vs. the white pixel. Maybe there's a word that changes that you could search for in the html like "Ok, that training complete" and you could look for the "Ok".

Maybe @CodeCruiser has an idea.
For an example,

 I have a program that gives me mousex, y and the color.

So far what it will do is tell me Coordinates 767, 86 Color: FFFFF9

I want to check the above Coordinates, for Color: FFFFF9 else do this instead.
Your (x, y) coordinates, are they absolute screen coords?...or relative to the upper left of a specific window?
They are screen coords.
Therein lies the problem with that method. There has to be a better way to detect when the page has updated to the blue than to look for the color of the button. Can yo share an example of the page with white and another with blue?
I could sendkey({F5}) to refreash the page?
so if true sendkey({F5}) else thread.sleep(5000)
I assume you'll be polling with a timer?

Here's how you can check if the color at an absolute screen coordinate is an exact specific color:
Private Function IsColorAt(ByVal pt As Point, ByVal clr As Color) As Boolean
        Dim bmp As New Bitmap(1, 1)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(pt, New Point(0, 0), bmp.Size)
        End Using
        Return bmp.GetPixel(0, 0).Equals(clr)
    End Function

Open in new window

So this function I would have to do the following with:

If IsColorAt() = True Then
 thread.sleep(3000)
else
 msgbox("Changed, site updated!")
endif

Private Function IsColorAt(ByVal pt As Point, ByVal clr As Color) As Boolean
        Dim bmp As New Bitmap(1, 1)
        Using G As Graphics = Graphics.FromImage(bmp)
            G.CopyFromScreen(pt, New Point(x coordinate, y coordinate), bmp.Size) ' x,y to check?
        End Using
        Return bmp.GetPixel(0, 0).Equals(clr)
    End Function

Open in new window

You had mentioned this combination previously:

    Coordinates 767, 86 Color: FFFFF9

Convert the hex color to a .Net Color and pass that along with the coords to the function:

    Dim pt As New Point(767. 86)
    Dim clr As Color = ... not sure how you convert that ...  (not a web guy!)

    If IsColorAt(pt, clr) = True Then
ok it's a RGB color, sorry for calling it hex was my mistake.  Not sure if that changes anything.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Tested and works like a OMG CHARM! You are the MAN, thank you so much for your help.