private CapturingThreadData _capturingThreadData; // data to exchange between form and capturing thread public Form1() { InitializeComponent(); _capturingThreadData = new CapturingThreadData(); cmbCapturingType.SelectedIndex = 0; }
public static void ThreadProc(Object obj) { CapturingThreadData data = (CapturingThreadData)obj; data.Success = true; // Prepare Capturer: Capturer capturer = new Capturer(); // create new screen capturer object capturer.RegistrationName = "demo"; capturer.RegistrationKey = "demo"; capturer.CaptureRectLeft = data.CaptureRectangle.Left; capturer.CaptureRectTop = data.CaptureRectangle.Top; capturer.CaptureRectWidth = data.CaptureRectangle.Width; capturer.CaptureRectHeight = data.CaptureRectangle.Height; capturer.OutputWidth = 640; capturer.OutputHeight = 480; // WMV and WEBM output use WMVVideoBitrate property to control output video bitrate // so try to increase it by x2 or x3 times if you think the output video are you are getting is laggy // capturer.WMVVideoBitrate = capturer.WMVVideoBitrate * 2; capturer.CaptureRectWidth = 320; capturer.CaptureRectHeight = 240; data.TempFile = Path.GetTempFileName(); data.TempFile = Path.ChangeExtension(data.TempFile, ".wmv"); capturer.OutputFileName = data.TempFile; capturer.CapturingType = data.CaptureType; // set border around captured area if we are not capturing entire screen if (capturer.CapturingType != CaptureAreaType.catScreen && capturer.CapturingType != CaptureAreaType.catWebcamFullScreen) { capturer.CaptureAreaBorderType = CaptureAreaBorderType.cabtDashed; capturer.CaptureAreaBorderColor = (uint)ColorTranslator.ToOle(Color.Red); } // Wait for events: WaitHandle[] events = new WaitHandle[] { data.StartOrResumeEvent, data.PauseEvent, data.StopEvent }; try { while (true) { int i = WaitHandle.WaitAny(events); if (events[i] == data.StartOrResumeEvent) { if (!capturer.IsRunning) { capturer.Run(); } } else if (events[i] == data.PauseEvent) { if (capturer.IsRunning) { capturer.Pause(); } } else if (events[i] == data.StopEvent) { capturer.Stop(); break; } } } catch (Exception ex) { data.ErrorText = ex.Message; data.Success = false; } finally { // Release resources Marshal.ReleaseComObject(capturer); } }