views:

3357

answers:

2

BACKGROUND

MY PROBLEM

  • Code works fine when the mouse cursor is the normal pointer or hand icon - the mouse is rendered correctly on the screenshot
  • However, when the mouse cursor is changed to the insertion point (the "I-beam" cursor) - for example typing in NOTEPAD - then code doesn't work - the result is that I get a faint image of the cursor - like a very translucent (gray) version of it instead of the blank & white one would expect.

MY QUESTION

  • How can I capture the mouse cursor image when the image is one of these "I-beam"-type images
  • NOTE: If you click on the original article someone offers a suggestion - it doesn't work

SOURCE

This is from the original article.

    static Bitmap CaptureCursor(ref int x, ref int y)
    {
        Bitmap bmp;
        IntPtr hicon;
        Win32Stuff.CURSORINFO ci = new Win32Stuff.CURSORINFO();
        Win32Stuff.ICONINFO icInfo;
        ci.cbSize = Marshal.SizeOf(ci);
        if (Win32Stuff.GetCursorInfo(out ci))
        {
            if (ci.flags == Win32Stuff.CURSOR_SHOWING)
            {
                hicon = Win32Stuff.CopyIcon(ci.hCursor);
                if (Win32Stuff.GetIconInfo(hicon, out icInfo))
                {
                    x = ci.ptScreenPos.x - ((int)icInfo.xHotspot);
                    y = ci.ptScreenPos.y - ((int)icInfo.yHotspot);

                    Icon ic = Icon.FromHandle(hicon);
                    bmp = ic.ToBitmap(); 
                    return bmp;
                }
            }
        }

        return null;
    }
+1  A: 

Your description of a translucent 'gray' version of the I-beam cursor makes me wonder if you're encountering an issue with image scaling or mispositioning of the cursor.

One of the people posting on that site provided a (broken) link to a report with peculiar behavior that I've tracked down to: http://www.efg2.com/Lab/Graphics/CursorOverlay.htm

The examples on that page are not in C# but the author of the codeproject solution may have been doing something similar and I know I've screwed up my scaling when using the graphics object on plenty of occassions myself:

In any ImageMouseDown event once an image is loaded, the CusorBitmap is drawn with transparency on top of the bitmap using the Canvas.Draw method. Note some coordinate adjustments (rescaling) are needed in case the bitmap is stretched to fit in the TImage.

nvuono
Scaling was my first instinct too
Overflow
+8  A: 
Tarsier
You can also use DrawIcon(hDC, x, y, hIcon) to directly draw the cursor to the target DC if you simply want to get a screenshot with the mouse cursor captured
fmuecke