Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 249563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:26:16+00:00 2026-05-11T21:26:16+00:00

BACKGROUND I am writing a screen capture application My code is based derived from

  • 0

BACKGROUND

  • I am writing a screen capture application
  • My code is based derived from this project: http://www.codeproject.com/KB/cs/DesktopCaptureWithMouse.aspx?display=Print
  • Note that the code captures the the mouse cursor also (which is desirable for me)

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:26:16+00:00Added an answer on May 11, 2026 at 9:26 pm

    While I can’t explain exactly why this happens, I think I can show how to get around it.

    The ICONINFO struct contains two members, hbmMask and hbmColor, that contain the mask and color bitmaps, respectively, for the cursor (see the MSDN page for ICONINFO for the official documentation).

    When you call GetIconInfo() for the default cursor, the ICONINFO struct contains both valid mask and color bitmaps, as shown below (Note: the red border has been added to clearly show the image boundaries):

    Default Cursor Mask Bitmap default cursor mask bitmap image

    Default Cursor Color Bitmap default cursor color bitmap image

    When Windows draws the default cursor, the mask bitmap is first applied with an AND raster operation, then the color bitmap is applied with an XOR raster operation. This results in an opaque cursor and a transparent background.

    When you call GetIconInfo() for the I-Beam cursor, though, the ICONINFO struct only contains a valid mask bitmap, and no color bitmap, as shown below (Note: again, the red border has been added to clearly show the image boundaries):

    I-Beam Cursor Mask Bitmap ibeam cursor mask bitmap image

    According to the ICONINFO documentation, the I-Beam cursor is then a monochrome cursor. The top half of the mask bitmap is the AND mask, and the bottom half of the mask bitmap is the XOR bitmap. When Windows draws the I-Beam cursor, the top half of this bitmap is first drawn over the desktop with an AND raster operation. The bottom half of the bitmap is then drawn over top with an XOR raster operation. Onscreen, The cursor will appear as the inverse of the content behind it.

    One of the comments for the original article that you linked mentions this. On the desktop, since the raster operations are applied over the desktop content, the cursor will appear correct. However, when the image is drawn over no background, as in your posted code, the raster operations that Windows performs result in a faded image.

    That being said, this updated CaptureCursor() method will handle both color and monochrome cursors, supplying a plain black cursor image when the cursor is monochrome.

    static Bitmap CaptureCursor(ref int x, ref int y)
    {
      Win32Stuff.CURSORINFO cursorInfo = new Win32Stuff.CURSORINFO();
      cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);
      if (!Win32Stuff.GetCursorInfo(out cursorInfo))
        return null;
    
      if (cursorInfo.flags != Win32Stuff.CURSOR_SHOWING)
        return null;
    
      IntPtr hicon = Win32Stuff.CopyIcon(cursorInfo.hCursor);
      if (hicon == IntPtr.Zero)
        return null;
    
      Win32Stuff.ICONINFO iconInfo;
      if (!Win32Stuff.GetIconInfo(hicon, out iconInfo))
        return null;
    
      x = cursorInfo.ptScreenPos.x - ((int)iconInfo.xHotspot);
      y = cursorInfo.ptScreenPos.y - ((int)iconInfo.yHotspot);
    
      using (Bitmap maskBitmap = Bitmap.FromHbitmap(iconInfo.hbmMask))
      {
        // Is this a monochrome cursor?
        if (maskBitmap.Height == maskBitmap.Width * 2)
        {
          Bitmap resultBitmap = new Bitmap(maskBitmap.Width, maskBitmap.Width);
    
          Graphics desktopGraphics = Graphics.FromHwnd(Win32Stuff.GetDesktopWindow());
          IntPtr desktopHdc = desktopGraphics.GetHdc();
    
          IntPtr maskHdc = Win32Stuff.CreateCompatibleDC(desktopHdc);
          IntPtr oldPtr = Win32Stuff.SelectObject(maskHdc, maskBitmap.GetHbitmap());
    
          using (Graphics resultGraphics = Graphics.FromImage(resultBitmap))
          {
            IntPtr resultHdc = resultGraphics.GetHdc();
    
            // These two operation will result in a black cursor over a white background.
            // Later in the code, a call to MakeTransparent() will get rid of the white background.
            Win32Stuff.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 32, Win32Stuff.TernaryRasterOperations.SRCCOPY);
            Win32Stuff.BitBlt(resultHdc, 0, 0, 32, 32, maskHdc, 0, 0, Win32Stuff.TernaryRasterOperations.SRCINVERT);
    
            resultGraphics.ReleaseHdc(resultHdc);
          }
    
          IntPtr newPtr = Win32Stuff.SelectObject(maskHdc, oldPtr);
          Win32Stuff.DeleteObject(newPtr);
          Win32Stuff.DeleteDC(maskHdc);
          desktopGraphics.ReleaseHdc(desktopHdc);
    
          // Remove the white background from the BitBlt calls,
          // resulting in a black cursor over a transparent background.
          resultBitmap.MakeTransparent(Color.White);
          return resultBitmap;
        }
      }
    
      Icon icon = Icon.FromHandle(hicon);
      return icon.ToBitmap();
    }
    

    There are some issues with the code that may or may not be a problem.

    1. The check for a monochrome cursor simply tests whether the height is twice the width. While this seems logical, the ICONINFO documentation does not mandate that only a monochrome cursor is defined by this.
    2. There is probably a better way to render the cursor that the BitBlt() – BitBlt() – MakeTransparent() combination of method calls I used.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 204k
  • Answers 204k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I was able to get it to build. Here is… May 12, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer This should work. Maybe add some further checking if not… May 12, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer You would need to create your own Lookup Table: You… May 12, 2026 at 8:50 pm

Related Questions

Given a file tree - a directory with directories in it etc, how would
I'm a Flex rookie tasked with enhancing an existing application. One of those enhancements
I have an application that needs to poll a webservice to see if the
Background I am writing a class library assembly in C# .NET 3.5 which is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.