Save Cursor as Bitmap

Posted on 2009-12-26
Last Modified: 2013-12-17
How to save the current Windows moise pointer (cursor) as a bitmap during application run-time using or C#?
Question by:jr_barros_jr
    LVL 4

    Accepted Solution

    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

    In VB.NET

    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

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

    How can i define Win32Stuff? Where is it?

    Thank you,



    Author Closing Comment

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

    Best regards.
    LVL 13

    Expert Comment

    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

    Thank you so much for this extra help.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now