title: Getting a pixel color at screen coordinates and detecting for color change
body: <p>I'm trying to create an application to perform some a task for me. Basically, I'd like to be able to assign a point on my screen with a hotkey (F12) which would save the color at that point.</p>
  
  <p>This pixel color changes and then resets back to its original color often.</p>
  
  <p>Throughout the runtime of the application, each time that color changes away from it's original color, it opens a msgbox letting me know that the color has changed again.</p>
  
  <p>I've done a lot of googling, but for the life of me, I can't figure this out.</p>
  
  <p>Here's what I have so far.</p>
  
  <p>I have a timer2 that detects my hotkey (F12), grabs the color, and even grabs the coordinates of my mouse:</p>
  
  <pre><code>Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
      For i = 1 To 255
          result = 0
          result = GetAsyncKeyState(i)
          If result = -32767 Then
              If i = 123 Then
                  Dim myBmp As New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
                  Dim g As Graphics = Graphics.FromImage(myBmp)
                  g.CopyFromScreen(Point.Empty, Point.Empty, myBmp.Size)
                  g.Dispose()
                  Label1.Text = MousePosition.X.ToString &amp; "," &amp; MousePosition.Y.ToString
                  PictureBox1.BackColor = myBmp.GetPixel(MousePosition.X, MousePosition.Y)
                  'Label1.BackColor = myBmp.GetPixel(MousePosition.X, MousePosition.Y)
                  myBmp.Dispose()
  
              End If
          End If
      Next i
  End Sub
  </code></pre>
  
  <p>Right now, that code errors out if I try to capture the pixel color on one of my other monitors (I have more than 1 monitor):<br>
  "An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in System.Drawing.dll
  Additional information: Parameter must be positive and &lt; Width."<br><br>
  at this line: PictureBox1.BackColor = myBmp.GetPixel(MousePosition.X, MousePosition.Y)</p>
  
  <p>Also, given that I am able to somewhat get the coordinates of the mouse, I have no idea how to keep my application monitoring those coordinates in a resource-light manner (without the mouse having to stay at that pixel location or the form being in focus) for the color changes. Can someone help please?</p>