Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the screen and mouse pointer using Windows APIs?

Tags:

c#

.net

graphics

I'm using the below code to capture the screen in a bitmap. The screen is captured, but I'm unable to get the mouse pointer on the screen. Could you suggest some alternative approach so that the mouse is captured as well?

    private Bitmap CaptureScreen()
    {
        // Size size is how big an area to capture
        // pointOrigin is the upper left corner of the area to capture
        int width = Screen.PrimaryScreen.Bounds.X + Screen.PrimaryScreen.Bounds.Width;
        int height = Screen.PrimaryScreen.Bounds.Y + Screen.PrimaryScreen.Bounds.Height;
        Size size = new Size(width, height);
        Point pointOfOrigin = new Point(0, 0);

        Bitmap bitmap = new Bitmap(size.Width, size.Height);
        {
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.CopyFromScreen(pointOfOrigin, new Point(0, 0), size);
            }
            return bitmap;
        }
    }
like image 653
Prahlad Yeri Avatar asked Jul 19 '11 15:07

Prahlad Yeri


People also ask

How do I screen capture with a cursor?

To take a screenshot that includes the mouse cursor, check the option labeled “Include Mouse Cursor” in the “Capture Setup” window, and then click “Start” at the bottom of the window. To capture screenshots with IrfanView, use the keyboard shortcut Control+F11.

What is SetCapture?

SetCapture captures mouse input either when the mouse is over the capturing window, or when the mouse button was pressed while the mouse was over the capturing window and the button is still down. Only one window at a time can capture the mouse.


1 Answers

[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;
}
like image 88
Dimitar Avatar answered Sep 21 '22 07:09

Dimitar