简体   繁体   English

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

[英]How can i capture the screen without the Form?

This is a class i'm using to capture my screen and mouse cursour as screenshot. 这是一个我用来捕捉我的屏幕和鼠标cursour作为截图的类。 But i want to make somehow that if the Form is in the middle of the screen don't capture it captrue the screen and the area behind the Form but not the Form it self. 但我想以某种方式表明,如果表单位于屏幕中间,则不会捕获屏幕和表单后面的区域,而不是表单自身。

Even if the form is in the front and i click on buttons or change something in the Form while the application is running do not capture it just keep capture the screen the area behind the Form like the Form is not there. 即使表单在前面,我点击按钮或在应用程序运行时更改窗体中的内容不要捕获它只是继续捕获屏幕表格后面的区域,如表格不存在。

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

What i mean is that i will see the Form when it's running and i will be able to change things click buttons but the captured screenshot if i will edit it with Paint i will not see the Form . 我的意思是,我将在运行时看到表单,我将能够更改单击按钮,但如果我将使用Paint编辑它,则捕获的屏幕截图将不会看到表单。

This is in Form1 how i make the capture: 这是Form1我如何进行捕获:

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

And timer1 tick event: 而timer1 tick事件:

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

This line make the actual capture: using (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true)) 此行进行实际捕获:使用(bitmap =(Bitmap)ScreenCapture.CaptureScreen(true))

Um.. Hide the form? 嗯..隐藏表格?

this.Visible = false; and THEN run the screenshot method. 然后运行截图方法。

Like this: 像这样:

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

and use it in your code the way you wanted: 并按照您想要的方式在代码中使用它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM