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

前端 未结 2 1809
天命终不由人
天命终不由人 2020-12-01 16:16

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 alternati

2条回答
  •  粉色の甜心
    2020-12-01 16:55

    If you're NOT looking for the EXACT replica of the cursor you're currently using, you could use the following code, all you have to do is add one line in your original code!

    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);
                //Following code is all you needed!
                graphics.DrawIcon(new Icon("Sample.ico"),Cursor.Position.X-50,Cursor.Position.Y-50);
                //The reason I minus 50 in the position is because you need to "offset" the position. Please go check out the post WholsRich commented.
            }
            return bitmap;
        }
    }
    

    You could go online and download all kind of icons.

    Or use ICO Convert to make your own.

    Good luck!

提交回复
热议问题