构建屏幕截图

时间:2016-06-21 04:42:36

标签: c# windows screen-capture video-recording

我正在尝试通过每隔x毫秒(然后将所有这些图像合并到AVI文件中)截取窗口的屏幕截图来构建特定窗口的简单视频录制,但我不知道如何定义x的值。我该如何定义它?这个常见的价值是什么?我读了一些关于24fps的内容。

我也不确定是否使用Timer,在Tick事件中进行捕获是一个好主意。我是否会有任何不准确之处,我应该使用别的东西?例如,由于任何原因,屏幕拍摄花费的时间比预期的要长。

我目前的实施是这样的:

  [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
   static extern bool GetWindowRect(IntPtr hWnd, out RECT r);

    public Bitmap GetScreenshot(IntPtr hwnd)
            {
                RECT rc;

                if (!GetWindowRect(hwnd, out rc))
                    throw new Win32Exception(Marshal.GetLastWin32Error());

                Bitmap bmp = new Bitmap(rc.right - rc.left, rc.bottom - rc.top, PixelFormat.Format32bppArgb);
                using (var gfxBmp = Graphics.FromImage(bmp))
                {
                    IntPtr hdcBitmap = gfxBmp.GetHdc();
                    bool succeeded = PrintWindow(hwnd, hdcBitmap, 0);
                    gfxBmp.ReleaseHdc(hdcBitmap);
                    if (!succeeded)
                    {
                        gfxBmp.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(Point.Empty, bmp.Size));
                    }
                    IntPtr hRgn = CreateRectRgn(0, 0, 0, 0);
                    GetWindowRgn(hwnd, hRgn);
                    Region region = Region.FromHrgn(hRgn);
                    if (!region.IsEmpty(gfxBmp))
                    {
                        gfxBmp.ExcludeClip(region);
                        gfxBmp.Clear(Color.Transparent);
                    }
                    return bmp;
                }
            }

    int i = 0;
    const string dest_path = @"C:\Users\pc2\Desktop\images";
    void doRecord()
    {
        string filename = Path.Combine(dest_path, string.Format("{0}.png", ++i));
         // yeah, I'll add some error checking here soon as it gets working.
        GetScreenshot(handle).Save(filename, ImageFormat.Png);
    }

在Timer的tick事件中我称之为:

private void timer1_Tick(object sender, EventArgs e)
        {
            doRecord();
        }

另外如何正确定义x的值,我错过了什么?

1 个答案:

答案 0 :(得分:1)

你必须设置'间隔'您正在使用的计时器的参数。 '间隔'是以毫秒为单位设置的,所以如果你想设置一个~24 FPS,那就是“间隔”。到42(1000(毫秒/秒)/ 24(期望的FPS)= 42)。