我如何在没有表单的情况下捕获屏幕?

本文关键字:情况下 屏幕 表单 | 更新日期: 2023-09-27 18:15:30

这是一个类,我用来捕捉我的屏幕和鼠标光标作为截图。但是我想做的是,如果窗体在屏幕中间,不要捕获它捕获屏幕和窗体后面的区域,而不是窗体本身。

即使表单在前面,当应用程序运行时,我点击按钮或更改表单中的某些内容,也不要捕获它,只捕获屏幕上表单后面的区域,就像表单不存在一样。

using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace ScreenShotDemo
{
    public class ScreenCapture
    {
        [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.Format32bppArgb);
            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;
        }
    }

我的意思是,当它运行时,我将看到表单,我将能够改变点击按钮的东西,但捕获的截图,如果我将用油漆编辑它,我将看不到表单。

这是在Form1中我是如何捕获的:

private void StartRecording_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

和timer1滴答事件:

private void timer1_Tick(object sender, EventArgs e)
        {
            using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))
            {
                ffmp.PushFrame(bitmap);
            }       
        }
使用(bitmap = (bitmap)ScreenCapture.CaptureScreen(true))

我如何在没有表单的情况下捕获屏幕?

嗯…隐藏表单?

this.Visible = false;,然后运行截图方法

:

protected Bitmap TakeScreenshot(bool cursor)
{
   Bitmap bitmap;
   this.Visible = false;
   bitmap = CaptureScreen(cursor);
   this.Visible = true;
   return bitmap;
}

并在你的代码中按你想要的方式使用它:

 private void timer1_Tick(object sender, EventArgs e)
 {
    using (bitmap = (Bitmap)ScreenCapture.TakeScreenshot(true))
    {
        ffmp.PushFrame(bitmap);
    }       
 }