1

My Aim:

  • I'd like to have the ability to retrieve the colour on the screen including the cursor, from certain coordinates.
  • In other words, I suppose you could say I'd like to retrieve the colour of the cursor (or at least, a certain point/pixel on the cursor).

My Difficulty:

  • I have some code already to retrieve the colour on the screen from certain coordinates, however I believe it does not include the cursor. I believe this to be because the method I'm using takes a screenshot (without the cursor), and reads colour from that.

My Current Code:

    public static Color RetrieveColour(Point Coordinates)
    {
        //Use this 'image' to create a Graphics class.
        using (Graphics Destination = Graphics.FromImage(ScreenPixel))
        {
             //Creates a graphic from the specified handler to a window.
             //In this case, it uses a handler which has been initialised to zero.
             using (Graphics Source = Graphics.FromHwnd(IntPtr.Zero))
             {
                 //Handler from the source device context.
                 IntPtr HandlerSourceDC = Source.GetHdc();
                 //Handler to the destination device context.
                 IntPtr HandlerDC = Destination.GetHdc();
                 //BitBlt is responsible for doing the copying.
                 //Returns a non-zero value for upon success.
                 int retval = BitBlt(HandlerDC, 0, 0, 1, 1, HandlerSourceDC, Coordinates.X, Coordinates.Y, (int)CopyPixelOperation.SourceCopy);
                 //Release the handlers.
                 Destination.ReleaseHdc();
                 Source.ReleaseHdc();
             }
        }
        //Retrieve the colour of the pixel at the specified coordinates.
        return ScreenPixel.GetPixel(0, 0);
    }

I should probably add, it should work with non-Windows cursors. By this I mean custom cursors from other applications (if that has any bearing on future answers).

1 Answers1

1

As far as I am currently aware, the only way that you'd be able to do this is if you were to create a custom driver that can act as an intermediary between the user.

I haven't read much of it, but you might want to look into this project http://www.codeproject.com/KB/cs/DesktopCaptureWithMouse.aspx?display=Print

Previously posted here: C# - Capturing the Mouse cursor image

Community
  • 1
  • 1
  • That's looking good, thanks. Only trouble is now, I need to somehow save the captured cursor so I may compare it with another. Don't suppose you'd know how this can be done? Short of actually saving the bmp itself. – Synthetic Ascension May 31 '15 at 09:31