|
-
Aug 21st, 2010, 02:02 PM
#1
Thread Starter
New Member
Detect Pixel color change
I'm trying to figure out some simple code to detect when a single pixel changes color at a location I choose -anywhere- on the screen. Maybe with just a msgbox popup when it changes.
The visual basic program will just be hidden and running in the background.
I should be able program the rest of the program around it, I just need the detection part. That is where I am stumped.
Any help would be greatly appreciated!!!
I'm using Visual Basic 2008
Thanks in advance!!!!!!!
-
Aug 21st, 2010, 02:30 PM
#2
Junior Member
Re: Detect Pixel color change
Here is some code to toy with for you. The form's background color takes the color of whatever pixel on the screen you point at. It is based on code to tyake a snapshot of the entire screen, and adopted a bit more to your question.
Code:
Public Class Form1
Public Declare Function GetPixel Lib "gdi32.dll" (ByVal hdc As IntPtr, _
ByVal nXPos As Int32, _
ByVal nYPos As Int32 _
) As Int32
Private Declare Function GetDC Lib "user32.dll" (ByVal hWnd As IntPtr) As IntPtr
Private Declare Function ReleaseDC Lib "user32.dll" (ByVal hWnd As IntPtr, _
ByVal hdc As IntPtr _
) As Int32
Private mainhdc = New IntPtr()
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
mainhdc = GetDC(IntPtr.Zero)
While True ' --- endless loop
BackColor = ColorTranslator.FromWin32(GetPixel(mainhdc, Cursor.Position.X, Cursor.Position.Y))
Application.DoEvents()
End While
End Sub
End Class
-
Aug 21st, 2010, 08:33 PM
#3
Re: Detect Pixel color change
No need for all those API functions!! Here's a method to detect the color in a specific area:
Code:
Private Function GetScreenColor(ByVal location As Point)
Dim bmp As New Bitmap(1, 1)
Using g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(location, Point.Empty, New Size(1, 1))
End Using
Return bmp.GetPixel(0, 0)
End Function
-
Dec 3rd, 2011, 02:02 PM
#4
Member
Re: Detect Pixel color change
sorry. how to use it if the color changes in the specific area and i want the mouse to move to other cor
-
Mar 29th, 2012, 11:19 PM
#5
Addicted Member
Re: Detect Pixel color change
How would i use the "GetScreenColor()" function?
GetScreenColor(What do i put here?)
I tried stuff like: GetScreenColor(100,200) for the x and y axis but not working:/
Last edited by therehere3; Mar 29th, 2012 at 11:53 PM.
-
Mar 30th, 2012, 12:00 AM
#6
Re: Detect Pixel color change
Of course it won't. The function is expecting a Point, not two values. A point is an X and Y coordinate, but in a single structure.
-
Mar 30th, 2012, 12:12 AM
#7
Addicted Member
Re: Detect Pixel color change
Im new but how would i make a point = 2 integers as x and y?
Dim MyPoint As Point = 100, 200
Would that work?
-
Mar 30th, 2012, 12:13 AM
#8
Re: Detect Pixel color change
Read the documentation that I linked to; it explains everything you need to know.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|