Click here to Skip to main content
11,924,800 members (53,375 online)
Click here to Skip to main content
Add your own
alternative version


93 bookmarked

Capturing the Desktop Screen with the Mouse Cursor Image

, 27 Jan 2006
Rate this:
Please Sign up or sign in to vote.
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.

You may also be interested in...

Comments and Discussions

QuestionDesktop Capture region Pin
Nicke Da Silva Manarin5-Dec-13 11:55
memberNicke Da Silva Manarin5-Dec-13 11:55 
QuestionCan I do it by C++? Pin
liaoyuandeyehuo8-May-13 19:41
memberliaoyuandeyehuo8-May-13 19:41 
QuestionRe: Can I do it by C++? Pin
manjav25-May-13 4:50
membermanjav25-May-13 4:50 
AnswerRe: Can I do it by C++? Pin
liaoyuandeyehuo30-May-13 1:12
memberliaoyuandeyehuo30-May-13 1:12 
GeneralMy vote of 5 Pin
Deepak Joy Jose18-Sep-12 22:14
memberDeepak Joy Jose18-Sep-12 22:14 
GeneralMy vote of 5 Pin
Spoolrd27-Jul-12 14:47
memberSpoolrd27-Jul-12 14:47 
QuestionDistant Learning App Pin
blarejazi26-Jun-12 12:34
memberblarejazi26-Jun-12 12:34 
QuestionCapture mouse point without desktop capture Pin
Member 912992816-Jun-12 9:17
memberMember 912992816-Jun-12 9:17 
GeneralMy vote of 5 Pin
(BlackBox) Ethical Hacker31-Oct-10 6:33
member(BlackBox) Ethical Hacker31-Oct-10 6:33 
GeneralHi Pin
FazFazFaz30-Sep-10 11:30
memberFazFazFaz30-Sep-10 11:30 
GeneralCan't capture cursor's shape like ' I ' and tip's text Pin
dong36423-Jul-10 18:37
memberdong36423-Jul-10 18:37 
GeneralNot working in game Pin
Shargon_8515-Apr-10 23:58
memberShargon_8515-Apr-10 23:58 
Generalvnc Pin
Nezam Ahamed21-Feb-10 19:34
memberNezam Ahamed21-Feb-10 19:34 
QuestionCopy using BitBlt? Pin
Lone Developer2-Sep-08 23:40
memberLone Developer2-Sep-08 23:40 
QuestionRemote Desktop Monitoring in C#.Net Pin
Member 371451227-Dec-07 4:15
memberMember 371451227-Dec-07 4:15 
AnswerRe: Remote Desktop Monitoring in C#.Net Pin
šprici4-Mar-10 5:33
memberšprici4-Mar-10 5:33 
QuestionVC++ :( Pin
Sobaan12-Jan-07 18:43
memberSobaan12-Jan-07 18:43 
QuestionText Cursor Pin
Fornazin11-Jan-07 10:29
memberFornazin11-Jan-07 10:29 
AnswerRe: Text Cursor Pin
Fornazin24-Jan-07 8:07
memberFornazin24-Jan-07 8:07 
QuestionIT is not rendering me desktop image with mouse Pin
gabrousman8-Jan-07 6:17
membergabrousman8-Jan-07 6:17 
QuestionHow can i control the remote Desktop Through Mouse and Key Board Pin
Me the Lover29-Dec-06 9:26
memberMe the Lover29-Dec-06 9:26 
QuestionHave you solved the IBeam cursor problem? Pin
Globulus15-Mar-06 2:44
memberGlobulus15-Mar-06 2:44 
AnswerRe: Have you solved the IBeam cursor problem? Pin
ssheldon7-Mar-07 18:31
memberssheldon7-Mar-07 18:31 
GeneralLeak in CaptureCursor found and fixed! Pin
Globulus14-Mar-06 5:51
memberGlobulus14-Mar-06 5:51 
GeneralRe: Leak in CaptureCursor found and fixed! Pin
ham-z9-Sep-07 18:01
memberham-z9-Sep-07 18:01 
GeneralRe: Leak in CaptureCursor found and fixed! Pin
Ghzanfar_Ali21-Mar-10 11:06
memberGhzanfar_Ali21-Mar-10 11:06 
GeneralExcellent Pin
mrsnipey14-Mar-06 0:39
membermrsnipey14-Mar-06 0:39 
QuestionLeak in CaptureCursor? Pin
Globulus12-Mar-06 8:07
memberGlobulus12-Mar-06 8:07 
AnswerRe: Leak in CaptureCursor? Pin
Rashid.Mahmood12-Mar-06 21:22
memberRashid.Mahmood12-Mar-06 21:22 
GeneralRe: Leak in CaptureCursor? Pin
Globulus12-Mar-06 22:42
memberGlobulus12-Mar-06 22:42 
GeneralRe: Leak in CaptureCursor? [modified] Pin
Twiggy Ramirez13-Jul-09 5:13
memberTwiggy Ramirez13-Jul-09 5:13 
GeneralWin32 and GDI Pin
Scott Elder5-Mar-06 18:03
memberScott Elder5-Mar-06 18:03 
GeneralRe: Win32 and GDI Pin
Rashid.Mahmood5-Mar-06 19:54
memberRashid.Mahmood5-Mar-06 19:54 
GeneralRe: Win32 and GDI Pin
Scott Elder5-Mar-06 20:06
memberScott Elder5-Mar-06 20:06 
AnswerRe: Win32 and GDI Pin
Razi Al-Sayed9-Apr-06 10:09
memberRazi Al-Sayed9-Apr-06 10:09 
GeneralSelected areas Pin
Michael J. Collins31-Jan-06 5:10
memberMichael J. Collins31-Jan-06 5:10 
Questionanother question :) Pin
ketan1430-Jan-06 23:05
memberketan1430-Jan-06 23:05 
AnswerRe: another question :) Pin
Rashid.Mahmood31-Jan-06 20:32
memberRashid.Mahmood31-Jan-06 20:32 
GeneralRe: another question :) Pin
ketan1413-Mar-06 1:10
memberketan1413-Mar-06 1:10 
GeneralRe: another question :) Pin
Daniel Repich31-May-06 12:04
memberDaniel Repich31-May-06 12:04 
Question"Desktop" ??? Pin
fwsouthern28-Jan-06 11:44
memberfwsouthern28-Jan-06 11:44 
AnswerRe: "Desktop" ??? Pin
Rashid.Mahmood29-Jan-06 20:14
memberRashid.Mahmood29-Jan-06 20: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 27 Jan 2006
Article Copyright 2006 by Rashid.Mahmood
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid