3

这是我用来捕获屏幕和鼠标光标作为屏幕截图的类。但是我想以某种方式使如果表单位于屏幕中间,则不要捕获它捕获屏幕和表单后面的区域,而不是捕获它本身的表单。

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

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;
        }
    }

我的意思是,当它运行时我会看到表单,并且我将能够更改单击按钮但捕获的屏幕截图,如果我将使用 Paint 对其进行编辑,我将看不到表单。

这是在 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))

4

1 回答 1

9

嗯..隐藏表格?

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);
    }       
 }
于 2013-05-26T14:04:44.927 回答