如何在没有表单的情况下捕获屏幕? [英] How can i capture the screen without the Form?

查看:34
本文介绍了如何在没有表单的情况下捕获屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用来捕获屏幕和鼠标光标作为屏幕截图的类.但我想以某种方式使如果窗体在屏幕中间不捕获它捕获屏幕和窗体后面的区域而不是它本身的窗体.

This is a class i'm using to capture my screen and mouse cursour as screenshot. 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;
        }
    }

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

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 .

这是在 Form1 中我如何进行捕获:

This is in Form1 how i make the capture:

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

和 timer1 滴答事件:

And timer1 tick event:

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

这一行进行实际捕获:使用 (bitmap = (Bitmap)ScreenCapture.CaptureScreen(true))

This line make the actual capture: using (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;
}

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

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

这篇关于如何在没有表单的情况下捕获屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆