我试图构建一个特定窗口的简单视频记录,方法是每隔x毫秒对该窗口截图一次(然后将所有这些图像合并到一个x文件中),但我不知道如何定义x的值。我该怎么定义它?有什么共同的价值习惯于此?我读了一些关于24 read的东西。
我也不确定是否使用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);
}在计时器的滴答事件中,我称之为:
private void timer1_Tick(object sender, EventArgs e)
{
doRecord();
}另外,如何正确定义x的值,我是不是遗漏了什么?
发布于 2016-06-21 04:48:24
您必须为正在使用的计时器设置“Interval”参数。“Interval”是以毫秒为单位设置的,因此,如果您想让一个~24个FPS将“Interval”设置为42 (每秒1000(毫秒)/ 24 (所需的FPS) = 42)。
https://stackoverflow.com/questions/37935731
复制相似问题