Click here to Skip to main content
15,392,442 members
Articles / Multimedia / GDI+
Posted 27 Jan 2006


100 bookmarked

Capturing the Desktop Screen with the Mouse Cursor Image

Rate me:
Please Sign up or sign in to vote.
4.58/5 (36 votes)
27 Jan 20063 min read
This artcile shows how to capture a desktop screenshot with the mouse cursor included.

Sample Image - DesktopCaptureWithMouse.jpg


This article shows how you can capture screen images including the mouse cursor.


Screen capturing is a very useful way of resource sharing as used in applications like Remote Desktop, Virtual Network Computing (VNC), where a user can access, view, and interact with a remote desktop as his own desktop. Also, it is used in non ethical applications like hacking applications, where a hacker can hack a computer using some malicious server application, and the server then frequently takes screenshots of the prey machine and sends them to the clients. You will see a lot of source code resources over the internet discussing how to take screenshots of the desktop or an area of the desktop but none of them discuss how to capture the mouse cursor bitmap with the screenshot. Sometimes it becomes necessary to capture the mouse to see the whole activity of the hacked machine. First, we will discuss here what the actual problem is.


Most of us think that the mouse cursor image is a part of the desktop display but actually it works on an upper layer over the desktop. Windows always tracks mouse with a “Hot Spot”, the actual position seen by the Windows.

Currently, there are two common ways to capture and manipulate the desktop image:

  1. Copy the desktop bitmap data from Video Memory to the System Memory. Do processing and then again blit it back to the Video Memory. This can be easily done using the BitBlt() or the StretchBlt() APIs provided by Win32.
  2. Another way is to directly manipulate the desktop bitmap in the Video Memory if enough memory is available as provided by DirectDraw.

Both of these don’t provide us the facility to capture the mouse cursor image with the desktop image.


The solution to the problem of capturing the mouse cursor image with the desktop image is quite simple.

  1. First, get the bitmap of the screen using BitBlt(). I have provided a simple function named CaptureDesktop() in the CaptureScreen.cs file that captures the screen bitmap as almost all the codes available over the internet do.
  2. Then capture the mouse cursor bitmap as:

    Get Cursor Icon:

    First, get the cursor information using the Win32 GetCursorInfo(). The function fills the CURSORINFO structure provided as a parameter. Don't forget to initialize the cbSize member of the structure before passing it as an argument. Then we check whether the cursor is visible or not, by checking for the equality of the flags member of the filled structure with the CURSOR_SHOWING constant. If they are equal then we get the handle to the cursor icon using the CopyIcon() function that takes the hCursor member of the above filled structure.

    Get Cursor Position:

    Now, we have to get the Icon information so that we can get the hotspot position. This information is easily retrieved using the GetIconInfo() function. Here is the C# implementation of the mouse capturing function:

    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;
  3. We now have both the bitmaps, i.e., the desktop bitmap and the mouse cursor bitmap with its position on the screen. Now, it's time to place the mouse cursor bitmap on the desktop bitmap. I have provided the following function that places the mouse cursor image over the desktop bitmap at the proper position:
    public static Bitmap CaptureDesktopWithCursor()
       int cursorX = 0;
       int cursorY = 0;
       Bitmap desktopBMP;
       Bitmap cursorBMP;
       Bitmap finalBMP;
       Graphics g;
       Rectangle r;
       desktopBMP = CaptureDesktop();
       cursorBMP = CaptureCursor(ref cursorX, ref cursorY);
       if(desktopBMP != null)
           if (cursorBMP != null)
               r = new Rectangle(cursorX, cursorY, 
                       cursorBMP.Width, cursorBMP.Height);
               g = Graphics.FromImage(desktopBMP);
               g.DrawImage(cursorBMP, r);
               return desktopBMP;
               return desktopBMP;
       return null;
  4. The bitmap with the cursor is now ready to be rendered over a viewer surface (a PictureBox used here). Since the viewer's visible area is smaller than the desktop bitmap area, scaling has been used in the function that displays the cooked desktop image.
    // ssWithMouseViewer is the PictureBox control
    private void Display(Bitmap desktop)
        Graphics g;
        Rectangle r;
        if(desktop != null)
            r = new Rectangle(0,0,ssWithMouseViewer.Width, 
            g = ssWithMouseViewer.CreateGraphics();


The binary image provided is compiled with Visual Studio .NET 2005 so you have to install .NET Framework 2.0.

Known problems

I have tested the application on my machine with no known problems, so I expect the same behavior on your machine.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Pakistan Pakistan
He is always interested to write technical articles that address the problems of the programmers community.

He loves to develope softares using C++, VC++ and C#. He also likes programming in Java due to its high portability. His area of interest is Graphics side esp. Games.

Comments and Discussions

AnswerRe: Have you solved the IBeam cursor problem? Pin
ssheldon7-Mar-07 17:31
Memberssheldon7-Mar-07 17:31 
GeneralLeak in CaptureCursor found and fixed! Pin
Globulus14-Mar-06 4:51
MemberGlobulus14-Mar-06 4:51 
GeneralRe: Leak in CaptureCursor found and fixed! Pin
ham-z9-Sep-07 17:01
Memberham-z9-Sep-07 17:01 
GeneralRe: Leak in CaptureCursor found and fixed! Pin
Ghzanfar_Ali21-Mar-10 10:06
MemberGhzanfar_Ali21-Mar-10 10:06 
GeneralExcellent Pin
mrsnipey13-Mar-06 23:39
Membermrsnipey13-Mar-06 23:39 
QuestionLeak in CaptureCursor? Pin
Globulus12-Mar-06 7:07
MemberGlobulus12-Mar-06 7:07 
AnswerRe: Leak in CaptureCursor? Pin
Rashid.Mahmood12-Mar-06 20:22
MemberRashid.Mahmood12-Mar-06 20:22 
GeneralRe: Leak in CaptureCursor? Pin
Globulus12-Mar-06 21:42
MemberGlobulus12-Mar-06 21:42 
Makes no difference. Only delays the inevitable.

I inserted some DoEvents/garbage collection into the loop. See below. Crashes after the same number of iterations. Added a few dozen DoEvents made no difference either. Calling a smaller loop multiple times (using a button, clicked manually, giving the system a chance to clean up) gave the same result as well: Crashes after around 4,975 total iterations.

I had a similar problem a few days ago (creating a cursor dynamically from a bitmap). Crashed consistently after around 1,200 iterations, so I had to find and fix the leak. Now I can call it millions of times without any problems. I hope to be able to do the same thing with your code. It goes a long way to solving my problem, but either a piece is missing or there is an InterOp problem that needs to be worked around.

Private Sub CaptureCursorLoop()
Dim totalmemory As String = ""
For i As Integer = 1 To 10000
Dim x As Integer
Dim y As Integer
Dim bm As Bitmap = ScreenshotCaptureWithMouse.ScreenCapture.CaptureScreen.CaptureCursor(x, y)

bm = Nothing

If i Mod 200 = 0 Then
totalmemory = GC.GetTotalMemory(False).ToString
End If

Me.Text = i.ToString & ": Memory: " & totalmemory

End Sub
GeneralRe: Leak in CaptureCursor? [modified] Pin
Twiggy Ramirez13-Jul-09 4:13
MemberTwiggy Ramirez13-Jul-09 4:13 
GeneralWin32 and GDI Pin
Scott Elder5-Mar-06 17:03
MemberScott Elder5-Mar-06 17:03 
GeneralRe: Win32 and GDI Pin
Rashid.Mahmood5-Mar-06 18:54
MemberRashid.Mahmood5-Mar-06 18:54 
GeneralRe: Win32 and GDI Pin
Scott Elder5-Mar-06 19:06
MemberScott Elder5-Mar-06 19:06 
AnswerRe: Win32 and GDI Pin
Razi Al-Sayed9-Apr-06 9:09
MemberRazi Al-Sayed9-Apr-06 9:09 
GeneralSelected areas Pin
Michael J. Collins31-Jan-06 4:10
professionalMichael J. Collins31-Jan-06 4:10 
Questionanother question :) Pin
ketan1430-Jan-06 22:05
Memberketan1430-Jan-06 22:05 
AnswerRe: another question :) Pin
Rashid.Mahmood31-Jan-06 19:32
MemberRashid.Mahmood31-Jan-06 19:32 
GeneralRe: another question :) Pin
ketan1413-Mar-06 0:10
Memberketan1413-Mar-06 0:10 
GeneralRe: another question :) Pin
Daniel Repich31-May-06 11:04
MemberDaniel Repich31-May-06 11:04 
Question"Desktop" ??? Pin
fwsouthern28-Jan-06 10:44
Memberfwsouthern28-Jan-06 10:44 
AnswerRe: "Desktop" ??? Pin
Rashid.Mahmood29-Jan-06 19:14
MemberRashid.Mahmood29-Jan-06 19:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.