Example #1
0
        private void StartInternal( RecordSettings settings ) {
            try {
                _stopwatch = _stopwatch ?? new Stopwatch();
                _stopwatch.Reset();
                _stopwatch.Start();
                StopWaiter.Reset();
                if ( !Directory.Exists( settings.OutputPath ) ) Directory.CreateDirectory( settings.OutputPath );
                while ( Recording ) {
                    var outfile = Path.Combine( settings.OutputPath, DateTime.Now.ToFileTime().ToString() + ".avi" );
                    using (var outstream = new VideoFileWriter()) {
                        var sourceRect = settings.CaptureRectangle;
                        var w = sourceRect.Width;
                        var h = sourceRect.Height;

                        outstream.Open(
                            outfile,
                            w,
                            h,
                            settings.Fps,
                            settings.Codec,
                            settings.Bitrate );

                        var mpf = settings.SplitInterval * 60000 / settings.Interval;
                        using ( var bmp = new Bitmap( w, h ) ) {
                            using ( var gr = Graphics.FromImage( bmp ) ) {
                                for ( var i = 0; (mpf==null||i < mpf) && Recording; i++ ) {
                                    try {
                                        gr.CopyFromScreen( sourceRect.X, sourceRect.Y, 0, 0, sourceRect.Size );
                                        PreprocessFrame( gr, settings );
                                        gr.Flush();
                                        outstream.WriteVideoFrame( bmp );
                                        settings.OnFrameWritten?.Invoke( _stopwatch.Elapsed );
                                    }
                                    catch { }
                                    Thread.Sleep( settings.Interval );
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex) {
                //global
            }
            finally {
                _stopwatch?.Stop();
                StopWaiter.Set();
            }
        }