Results 1 to 8 of 8

Thread: Detect Pixel color change

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    1

    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!!!!!!!

  2. #2
    Junior Member
    Join Date
    Dec 2008
    Posts
    29

    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
    more tech mumbo jumbo @ http://www.noordam.IT

  3. #3
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    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

  4. #4
    Member
    Join Date
    Mar 2008
    Posts
    39

    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

  5. #5
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    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.

  6. #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.

  7. #7
    Addicted Member
    Join Date
    Jul 2011
    Posts
    245

    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?

  8. #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
  •  



Click Here to Expand Forum to Full Width