Мое приложение WinForms.NET 4 C #записывает рабочий стол, когда пользователь взаимодействует с ним.
Он использует оболочки AForge FFMPEG или VFW в зависимости от скорости системы. Конечно, захват выполняется в фоновом потоке.
В любом случае обертки требуют, чтобы частота кадров была указана заранее. Это проблематично, поскольку частота захвата не является детерминированной и легко зависит от того, насколько загружена целевая машина. На хорошей системе я получаю максимум 10 FPS.
Итак, здесь два вопроса.:
Код, который я использую, приведен ниже для ясности:
private void ThreadWork ()
{
int count = 0;
string filename = "";
RECT rect = new RECT();
System.Drawing.Bitmap bitmap = null;
System.Drawing.Graphics graphics = null;
System.DateTime dateTime = System.DateTime.Now;
System.IntPtr hWndForeground = System.IntPtr.Zero;
AForge.Video.FFMPEG.VideoFileWriter writer = null;
filename = @"c:\Users\Raheel Khan\Desktop\Temp\Capture.avi";
if (System.IO.File.Exists(filename))
System.IO.File.Delete(filename);
writer = new AForge.Video.FFMPEG.VideoFileWriter();
writer.Open
(
filename,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
10, // FPS
AForge.Video.FFMPEG.VideoCodec.MPEG4
);
bitmap = new Bitmap
(
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb
);
graphics = System.Drawing.Graphics.FromImage(bitmap);
dateTime = System.DateTime.Now;
while (System.DateTime.Now.Subtract(dateTime).TotalSeconds < 10) // 10 seconds.
{
graphics.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size);
rect = new RECT();
hWndForeground = GetForegroundWindow();
GetWindowRect(hWndForeground, ref rect);
graphics.DrawRectangle(Pens.Red, rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
graphics.DrawString(count++.ToString(), this.Font, Brushes.Red, 10, 10);
writer.WriteVideoFrame(bitmap);
System.Threading.Thread.Sleep(10);
}
writer.Close();
writer.Dispose();
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(filename));
}