public static void ThreadProc(Object obj)
        {
            Capturer capturer = new Capturer(); // create new screen capturer object

            CapturingThreadData data = (CapturingThreadData)obj;

            if (Program.Cfg.WriteLog)
            {
                capturer.SetLogFile(Path.GetTempPath() + Application.ProductName + " log.txt");
            }

            capturer.RegistrationName = "demo";
            capturer.RegistrationKey  = "demo";

            if (Program.Cfg.AudioDevice != "")
            {
                capturer.CurrentAudioDeviceName = Program.Cfg.AudioDevice;
            }

            if (Program.Cfg.AudioLine != "")
            {
                capturer.CurrentAudioDeviceLineName = Program.Cfg.AudioLine;
            }

            if (Program.Cfg.SelectedVideoCodecTab == 0)
            {
                capturer.CurrentWMVAudioCodecName = Program.Cfg.WmvAudioCodec;
                capturer.CurrentWMVAudioFormat    = Program.Cfg.WmvAudioFormat;
                capturer.CurrentWMVVideoCodecName = Program.Cfg.WmvVideoCodec;

                Program.Cfg.WmvAudioCodec  = capturer.CurrentWMVAudioCodecName;
                Program.Cfg.WmvAudioFormat = capturer.CurrentWMVAudioFormat;
                Program.Cfg.WmvVideoCodec  = capturer.CurrentWMVVideoCodecName;
            }
            else
            {
                capturer.CurrentAudioCodecName = Program.Cfg.AviAudioCodec;
                capturer.CurrentVideoCodecName = Program.Cfg.AviVideoCodec;
            }

            capturer.AudioEnabled = Program.Cfg.EnableAudio;
            // this option tells to use captured area dimensions as output video width/height
            // or use user defined video dimensions
            capturer.MatchOutputSizeToTheSourceSize = !Program.Cfg.ResizeOutputVideo;
            capturer.FPS = Program.Cfg.FPS;

            capturer.ShowMouseHotSpot                    = Program.Cfg.ShowMouseHotSpot;
            capturer.CaptureMouseCursor                  = Program.Cfg.CaptureMouseCursor;
            capturer.AnimateMouseClicks                  = Program.Cfg.AnimateMouseClicks;
            capturer.AnimateMouseButtons                 = Program.Cfg.AnimateMouseButtons;
            capturer.MouseAnimationDuration              = Program.Cfg.MouseAnimationDuration;
            capturer.MouseSpotRadius                     = Program.Cfg.MouseSpotRadius;
            capturer.MouseHotSpotColor                   = (uint)ColorTranslator.ToOle(Program.Cfg.MouseHotSpotColor);
            capturer.MouseCursorLeftClickAnimationColor  = (uint)ColorTranslator.ToOle(Program.Cfg.MouseCursorLeftClickAnimationColor);
            capturer.MouseCursorRightClickAnimationColor = (uint)ColorTranslator.ToOle(Program.Cfg.MouseCursorRightClickAnimationColor);

            capturer.CaptureRectLeft   = data.CaptureRectangle.Left;
            capturer.CaptureRectTop    = data.CaptureRectangle.Top;
            capturer.CaptureRectWidth  = data.CaptureRectangle.Width;
            capturer.CaptureRectHeight = data.CaptureRectangle.Height;

            capturer.KeepAspectRatio = Program.Cfg.KeepAspectRatio;

            // show recording time stamp
            capturer.OverlayingRedTextCaption = "Recording: {RUNNINGMIN}:{RUNNINGSEC}:{RUNNINGMSEC} on {CURRENTYEAR}-{CURRENTMONTH}-{CURRENTDAY} at {CURRENTHOUR}:{CURRENTMIN}:{CURRENTSEC}:{CURRENTMSEC}";

            capturer.OutputWidth  = Program.Cfg.OutputWidth;
            capturer.OutputHeight = Program.Cfg.OutputHeight;

            if ((capturer.WebCamCount > 0) && (data.ShowWebCamStream))
            {
                capturer.AddWebCamVideo = true;

                if (!String.IsNullOrEmpty(Program.Cfg.WebCameraDevice))
                {
                    capturer.CurrentWebCamName = Program.Cfg.WebCameraDevice;
                }

                capturer.SetWebCamVideoRectangle(Program.Cfg.WebCameraWindowX, Program.Cfg.WebCameraWindowY, Program.Cfg.WebCameraWindowWidth, Program.Cfg.WebCameraWindowHeight);
            }

            data.TempFile           = Path.GetTempFileName();
            data.TempFile           = Path.ChangeExtension(data.TempFile, (Program.Cfg.SelectedVideoCodecTab == 0) ? ".wmv" : ".avi");
            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)
            {
                // set border style
                capturer.CaptureAreaBorderType  = Program.Cfg.CaptureAreaBorderType;
                capturer.CaptureAreaBorderColor = (uint)ColorTranslator.ToOle(Program.Cfg.CaptureAreaBorderColor);
                capturer.CaptureAreaBorderWidth = Program.Cfg.CaptureAreaBorderWidth;
            }


            try
            {
                capturer.Run();

                // IMPORTANT: if you want to check for some code if need to stop the recording then make sure you are
                // using Thread.Sleep(1) inside the checking loop, so you have the loop like
                // Do
                // Thread.Sleep(1)
                // While StopButtonNotClicked
            }
            catch (COMException ex)
            {
                data.ErrorText = ex.Message;
                data.Result    = 1;
                Marshal.ReleaseComObject(capturer);
                return;
            }

            try
            {
                Thread.Sleep(Timeout.Infinite);
            }
            catch (ThreadInterruptedException)
            {
                capturer.Stop();
                data.Result = 0;
            }
            catch (Exception ex)
            {
                data.ErrorText = ex.Message;
                data.Result    = 1;
            }
            finally
            {
                Marshal.ReleaseComObject(capturer);
            }
        }
        private void btnStartRecording_Click(object sender, System.EventArgs e)
        {
            // Set up the capturer

            if (cbLog.Checked)
            {
                capturer.SetLogFile(Path.GetTempPath() + "Capturing Log.txt");
            }

            bool captureToAvi = (tabControl1.SelectedIndex == 1);

            capturer.AudioEnabled           = cbEnableAudio.Checked;
            capturer.CurrentAudioDeviceName = cmbAudioDevices.SelectedItem.ToString();
            if (cmbAudioLines.Items.Count > 0)
            {
                capturer.CurrentAudioDeviceLineName = cmbAudioLines.SelectedItem.ToString();
            }

            if (captureToAvi)
            {
                capturer.CurrentAudioCodecName = cmbAviAudioCodecs.SelectedItem.ToString();
                capturer.CurrentVideoCodecName = cmbAviVideoCodecs.SelectedItem.ToString();
            }
            else             // capture to WMV
            {
                capturer.CurrentWMVAudioCodecName = cmbWmvAudioCodecs.SelectedItem.ToString();
                capturer.CurrentWMVAudioFormat    = cmbWmvAudioFormats.SelectedIndex;
                capturer.CurrentWMVVideoCodecName = cmbWmvVideoCodecs.SelectedItem.ToString();
            }

            capturer.MouseAnimationDuration = 400;
            capturer.FPS = float.Parse(cmbFPS.Text);
            capturer.CaptureTransparentControls = cbCaptureLayeredWindows.Checked;
            capturer.MouseSpotRadius            = (int)nudMouseHotSpot.Value;

            capturer.OutputWidth  = int.Parse(tbWidth.Text);
            capturer.OutputHeight = int.Parse(tbHeight.Text);

            // Set capturing mode

            if (rbRectRegion.Checked)
            {
                capturer.CapturingType = CaptureAreaType.catRegion;
            }
            else if (rbRegionAroundMouse.Checked)
            {
                capturer.CapturingType = CaptureAreaType.catMouse;
            }
            else
            {
                capturer.CapturingType = CaptureAreaType.catScreen;
            }

            if (cbShowWebCamOverlay.Checked)
            {
                // Set web camera device to add overlaying video to the source
                capturer.AddWebCamVideo = true;

                // Set device name or you can set device by index using .CurrentCaptureDevice property
                capturer.CurrentWebCam = cmbWebCameras.SelectedIndex;

                // Set rectangle to show overlaying video from webcam into the rectangle 160x120 (starting with left point at 10,10)
                capturer.SetWebCamVideoRectangle(
                    int.Parse(tbWebCameraX.Text),
                    int.Parse(tbWebCameraY.Text),
                    int.Parse(tbWebCameraWidth.Text),
                    int.Parse(tbWebCameraHeight.Text));
            }


            // set border around captured area if we are not capturing entire screen
            if (
                capturer.CapturingType != CaptureAreaType.catScreen &&
                capturer.CapturingType != CaptureAreaType.catWebcamFullScreen
                )
            {
                // set border style
                capturer.CaptureAreaBorderType  = CaptureAreaBorderType.cabtDashed;
                capturer.CaptureAreaBorderColor = (uint)ColorTranslator.ToOle(Color.Red);
            }

            // Save to temp file

            String filename = Path.GetTempFileName();

            filename = Path.ChangeExtension(filename, captureToAvi ? "avi" : "wmv");
            capturer.OutputFileName = filename;

            try
            {
                // Start capturing
                try
                {
                    Cursor = Cursors.WaitCursor;
                    capturer.Run();
                }
                finally
                {
                    Cursor = Cursors.Default;
                }

                _recording = true;
                btnStartRecording.Enabled = false;
                btnPauseRecording.Enabled = true;
                btnStopRecording.Enabled  = true;

                // Wait for "Stop Recording" button pressed
                while (_recording)
                {
                    Thread.Sleep(10);
                    Application.DoEvents();
                }

                // Stop capturing
                try
                {
                    Cursor = Cursors.WaitCursor;
                    capturer.Stop();
                }
                finally
                {
                    Cursor = Cursors.Default;
                }

                // Ask for output file name
                SaveFileDialog dlg = new SaveFileDialog();

                dlg.Title    = @"Save captured video as";
                dlg.FileName = @"Screencast";

                if (captureToAvi)
                {
                    dlg.DefaultExt = @"*.avi";
                    dlg.Filter     = @"AVI files (*.avi)|*.avi|All files (*.*)|*.*";
                }
                else
                {
                    dlg.DefaultExt = @"*.wmv";
                    dlg.Filter     = @"WMV files (*.wmv)|*.wmv|All files (*.*)|*.*";
                }

                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    // Save to specified file name
                    File.Copy(capturer.OutputFileName, dlg.FileName, true);
                }

                // clean up
                try
                {
                    File.Delete(capturer.OutputFileName);
                }
                catch
                {
                }

                btnStartRecording.Enabled = true;
                btnPauseRecording.Enabled = false;
                btnStopRecording.Enabled  = false;

                // open saved video in default video viewer
                try
                {
                    Process.Start(dlg.FileName);
                }
                catch
                {
                }
            }
            catch (COMException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }