Okay i'll go through each section in detail for you.
First step. declarations:
[php]
Private Declare Function GetPixel Lib "Gdi32.dll" (ByVal hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer
Private Declare Function CreateDC Lib "Gdi32.dll" (ByVal driverName As String, ByVal deviceName As String, ByVal output As String, ByVal lpInitData As IntPtr) As IntPtr
Private Declare Function DeleteDC Lib "Gdi32.dll" (ByVal dc As IntPtr) As Boolean
Dim ScreenDC As IntPtr
[/php]
The GetPixel function gets the color of a pixel at the specified coordinates and stores it as an integer value. For this to work you need a device context ("ByVal hdc", in this case we use "ScreenDC" which we assign to the display of your monitor) It also needs an x and y coordinate to get the pixel from ("ByVal x As Integer, ByVal y As Integer")
CreateDC creates a device context and DeleteDC deletes the device context. We need to get the DC of the display so we can scan it. On closing the program we need to release the DC.
ScreenDC is a global variable of type "IntPtr", we'll assign this variable a value in the form_load event.
Okay now that's declarations out of the way, now we need to get that value for ScreenDC
[php]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ScreenDC = CreateDC("DISPLAY", Nothing, Nothing, CType(Nothing, IntPtr))
End Sub
[/php]
Essentially what this does is assign the variable "ScreenDC" the device context of the monitor's display ("CreateDC("DISPLAY".....)")
Okay now we've got the ScreenDC, yay! Before we continue we have to remember to release the DC on form close so add this before we forget!
[php]
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
DeleteDC(ScreenDC)
End Sub
[/php]
This will release the device context specified by the variable "ScreenDC"
Okay so we've got the ScreenDC, now we can set up a PixelScanner function, like so:
[php]
Function PixelScan(ByVal startX As Integer, ByVal startY As Integer, ByVal toX As Integer, ByVal toY As Integer, ByVal sColor As System.Drawing.Color)
Dim thisPixel As Integer
Dim PixelColor As System.Drawing.Color
For x As Integer = startX To toX
For y As Integer = startY To toY
thisPixel = GetPixel(ScreenDC, x, y)
PixelColor = ColorTranslator.FromOle(thisPixel)
If PixelColor = sColor Then
'do whatever you want when the color is found here.
End If
Next
Next
Return Nothing
End Function
[/php]
Okay basically what this is doing:
The function "PixelScan" accepts 5 arguments; "StartX", "StartY", "ToX", "ToY" and "sColor".
Envision the area you're scanning as a big box, specified by the "StartX", "StartY", "ToX", "ToY" variables. The StartX and StartY values tell the function what location to start the scan from (i.e putting 0 for both StartX and StartY will start the scan at the top left corner of your screen. Now you need to tell it where to stop scanning for both the X and Y coords, that pretty much sets up your box. Now for the last argument "sColor", this is the color we are going to compare each pixel to (i.e the color you are searching for)
Now for the main part of the function.
Basically, to break this down into English terminology:
For each value of X between the two coordinates of x specified by "StartX" and "ToX", get every Y coordinate specified between "StartY" and "ToY". Now you have 2 coordinates, x and y. Now we employ the "GetPixel" function to get the color at this x/y coordinate. Remember, GetPixel outputs an integer, not a System.Drawing.Color. This is why we use a ColorTranslator to translate this integer into a color we can compare with our argument "sColor".
Because this is a for/next it will continue until all the pixels are scanned. Basically you have your first "For/Next" which is the x values. Contained in this is the For/Next for y values. Basically what this is saying is, get an x value between "StartX" and "ToX", for this x value check every y value. Once that is complete the next x value will be chosen and the process will continue until all pixels in the area are scanned.
In that snippet i have included a
[php]
'do what you want to happen when the colors match here
[/php]
Basically I have said, if the color of the currently scanned pixel is the same as the color specified by the argument "sColor", do something.
Okay now all you need to do is scan.
The following snippet will scan your whole screen (remember, your screen resolution is the number of pixels across and up your monitor):
[php]
PixelScan(0, 0, My.Computer.Screen.WorkingArea.Width, My.Computer.Screen.WorkingArea.Height, Color.Blue)
[/php]
Basically this is saying start scanning at the point (0, 0) and continue the entire screen has been scanned, the color to compare to is the color blue, if this color is found the function will do whatever you've told it to do when this color is found. Note, you don't need to use "My.Computer.Screen.WorkingArea..." all the time, that's just a quick way to tell it to scan the entire width and height of your screen, you can specify exactly what section to scan by putting an integer value in there like :
[php]
PixelScan(100, 100, 200, 200, color.black)
[/php]
I hope this long ass post helps you out, took ages to type out haha.
Good luck.