C# - Capturing The Mouse Cursor Image


Answer :

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.

[StructLayout(LayoutKind.Sequential)] struct CURSORINFO {     public Int32 cbSize;     public Int32 flags;     public IntPtr hCursor;     public POINTAPI ptScreenPos; }  [StructLayout(LayoutKind.Sequential)] struct POINTAPI {     public int x;     public int y; }  [DllImport("user32.dll")] static extern bool GetCursorInfo(out CURSORINFO pci);  [DllImport("user32.dll")] static extern bool DrawIcon(IntPtr hDC, int X, int Y, IntPtr hIcon);  const Int32 CURSOR_SHOWING = 0x00000001;  public static Bitmap CaptureScreen(bool CaptureMouse) {     Bitmap result = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format24bppRgb);      try     {         using (Graphics g = Graphics.FromImage(result))         {             g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);              if (CaptureMouse)             {                 CURSORINFO pci;                 pci.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(CURSORINFO));                  if (GetCursorInfo(out pci))                 {                     if (pci.flags == CURSOR_SHOWING)                     {                         DrawIcon(g.GetHdc(), pci.ptScreenPos.x, pci.ptScreenPos.y, pci.hCursor);                         g.ReleaseHdc();                     }                 }             }         }     }     catch     {         result = null;     }      return result; } 

Here's a modified version of Dimitar's response (using DrawIconEx) that worked for me on multiple screens:

public class ScreenCapturePInvoke {     [StructLayout(LayoutKind.Sequential)]     private struct CURSORINFO     {         public Int32 cbSize;         public Int32 flags;         public IntPtr hCursor;         public POINTAPI ptScreenPos;     }      [StructLayout(LayoutKind.Sequential)]     private struct POINTAPI     {         public int x;         public int y;     }      [DllImport("user32.dll")]     private static extern bool GetCursorInfo(out CURSORINFO pci);      [DllImport("user32.dll", SetLastError = true)]     static extern bool DrawIconEx(IntPtr hdc, int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyHeight, int istepIfAniCur, IntPtr hbrFlickerFreeDraw, int diFlags);      private const Int32 CURSOR_SHOWING = 0x0001;     private const Int32 DI_NORMAL = 0x0003;      public static Bitmap CaptureFullScreen(bool captureMouse)     {         var allBounds = Screen.AllScreens.Select(s => s.Bounds).ToArray();         Rectangle bounds = Rectangle.FromLTRB(allBounds.Min(b => b.Left), allBounds.Min(b => b.Top), allBounds.Max(b => b.Right), allBounds.Max(b => b.Bottom));          var bitmap = CaptureScreen(bounds, captureMouse);         return bitmap;     }      public static Bitmap CapturePrimaryScreen(bool captureMouse)     {         Rectangle bounds = Screen.PrimaryScreen.Bounds;          var bitmap = CaptureScreen(bounds, captureMouse);         return bitmap;     }      public static Bitmap CaptureScreen(Rectangle bounds, bool captureMouse)     {         Bitmap result = new Bitmap(bounds.Width, bounds.Height);          try         {             using (Graphics g = Graphics.FromImage(result))             {                 g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size);                  if (captureMouse)                 {                     CURSORINFO pci;                     pci.cbSize = Marshal.SizeOf(typeof (CURSORINFO));                      if (GetCursorInfo(out pci))                     {                         if (pci.flags == CURSOR_SHOWING)                         {                             var hdc = g.GetHdc();                             DrawIconEx(hdc, pci.ptScreenPos.x-bounds.X, pci.ptScreenPos.y-bounds.Y, pci.hCursor, 0, 0, 0, IntPtr.Zero, DI_NORMAL);                             g.ReleaseHdc();                         }                     }                 }             }         }         catch         {             result = null;         }          return result;     } } 

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable