Save Cursor as Bitmap

Posted on 2009-12-26
Medium Priority
Last Modified: 2013-12-17
How to save the current Windows moise pointer (cursor) as a bitmap during application run-time using VB.net or C#?
Question by:jr_barros_jr
  • 3
  • 2

Accepted Solution

liorfr earned 1000 total points
ID: 26125468
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;
LVL 13

Assisted Solution

Ashok earned 1000 total points
ID: 26125476

Shared Function CaptureCursor(ByRef x As Integer, ByRef y As Integer) As Bitmap
      Dim bmp As Bitmap
      Dim hicon As IntPtr
      Dim ci As New Win32Stuff.CURSORINFO()
      Dim icInfo As Win32Stuff.ICONINFO
      ci.cbSize = Marshal.SizeOf(ci)
      If Win32Stuff.GetCursorInfo(ci) Then
            If ci.flags = Win32Stuff.CURSOR_SHOWING Then
                  hicon = Win32Stuff.CopyIcon(ci.hCursor)
                  If Win32Stuff.GetIconInfo(hicon, icInfo) Then
                        x = ci.ptScreenPos.x - (DirectCast(icInfo.xHotspot, Integer))
                        y = ci.ptScreenPos.y - (DirectCast(icInfo.yHotspot, Integer))
                        Dim ic As Icon = Icon.FromHandle(hicon)
                        bmp = ic.ToBitmap()

                        Return bmp
                  End If
            End If
      End If
      Return Nothing
End Function


Author Comment

ID: 26126019
I am having "Error    2    Type 'Win32Stuff.CURSORINFO' is not defined."

How can i define Win32Stuff? Where is it?

Thank you,


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Author Closing Comment

ID: 31670105
To avoid discussion, I would like to congratulate both for the help.

Best regards.
LVL 13

Expert Comment

ID: 26126101
Here is source code for the Win32Stuff class

Class Win32Stuff

      #region Class Variables

      Public Const SM_CXSCREEN As Integer = 0
      Public Const SM_CYSCREEN As Integer = 1

      Public Const CURSOR_SHOWING As Int32 = &H1

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure ICONINFO
            Public fIcon As Boolean
            ' Specifies whether this structure defines an icon or a cursor. A value of TRUE specifies
            Public xHotspot As Int32
            ' Specifies the x-coordinate of a cursor's hot spot. If this structure defines an icon, the hot
            Public yHotspot As Int32
            ' Specifies the y-coordinate of the cursor's hot spot. If this structure defines an icon, the hot
            Public hbmMask As IntPtr
            ' (HBITMAP) Specifies the icon bitmask bitmap. If this structure defines a black and white icon,
            Public hbmColor As IntPtr
            ' (HBITMAP) Handle to the icon color bitmap. This member can be optional if this
      End Structure
      <StructLayout(LayoutKind.Sequential)> _
      Public Structure POINT
            Public x As Int32
            Public y As Int32
      End Structure

      <StructLayout(LayoutKind.Sequential)> _
      Public Structure CURSORINFO
            Public cbSize As Int32
            ' Specifies the size, in bytes, of the structure.
            Public flags As Int32
            ' Specifies the cursor state. This parameter can be one of the following values:
            Public hCursor As IntPtr
            ' Handle to the cursor.
            Public ptScreenPos As POINT
            ' A POINT structure that receives the screen coordinates of the cursor.
      End Structure
#End Region

      #region Class Functions

      <DllImport("user32.dll", EntryPoint := "GetDesktopWindow")> _
      Public Shared Function GetDesktopWindow() As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetDC")> _
      Public Shared Function GetDC(ptr As IntPtr) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetSystemMetrics")> _
      Public Shared Function GetSystemMetrics(abc As Integer) As Integer
      End Function

      <DllImport("user32.dll", EntryPoint := "GetWindowDC")> _
      Public Shared Function GetWindowDC(ptr As Int32) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "ReleaseDC")> _
      Public Shared Function ReleaseDC(hWnd As IntPtr, hDc As IntPtr) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetCursorInfo")> _
      Public Shared Function GetCursorInfo(ByRef pci As CURSORINFO) As Boolean
      End Function

      <DllImport("user32.dll", EntryPoint := "CopyIcon")> _
      Public Shared Function CopyIcon(hIcon As IntPtr) As IntPtr
      End Function

      <DllImport("user32.dll", EntryPoint := "GetIconInfo")> _
      Public Shared Function GetIconInfo(hIcon As IntPtr, ByRef piconinfo As ICONINFO) As Boolean
      End Function
#End Region
End Class


Author Comment

ID: 26126764
Thank you so much for this extra help.

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question