private void StartRecording(CaptureAreaType captureType, bool withCam)
        {
            _recording = true;

            if (withCam)
            {
                _dropDownItems    = tsbRecordWithCam.DropDown;
                tsbRecord.Enabled = false;
                tsbRecordWithCam.ShowDropDownArrow = false;
                tsbRecordWithCam.DropDown          = null;
                tsbRecordWithCam.Image             = Resources.stop;
                tsbRecordWithCam.Text = "Stop";
            }
            else
            {
                _dropDownItems              = tsbRecord.DropDown;
                tsbRecordWithCam.Enabled    = false;
                tsbRecord.ShowDropDownArrow = false;
                tsbRecord.DropDown          = null;
                tsbRecord.Image             = Resources.stop;
                tsbRecord.Text              = "Stop";
            }

            statusBar.Items[0].Text = "Recording started";
            tsbPlay.Enabled         = false;

            if (!Program.Cfg.DoNotShowMessage1)
            {
                CustomMessageBox mb = new CustomMessageBox(Application.ProductName, "Press CTRL-SHIFT-F12 to stop recording.");
                mb.StartPosition = FormStartPosition.CenterScreen;
                mb.ShowDialog();
                Program.Cfg.DoNotShowMessage1 = mb.DoNotShow;
            }

            RegisterHotKey(Handle, MYKEYID, MOD_CONTROL + MOD_SHIFT, Keys.F12);

            _capturingThreadData = new CapturingThreadData();

            _capturingThreadData.CaptureType      = captureType;
            _capturingThreadData.CaptureRectangle = _recordingRegion;
            _capturingThreadData.ShowWebCamStream = withCam;

            _capturingThread = new Thread(CapturingThread.ThreadProc);
            _capturingThread.Start(_capturingThreadData);

            WindowState = FormWindowState.Minimized;
        }
        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);
            }
        }