Example #1
0
        void Record()
        {
            try
            {
                var  lastFrameWriteTime = DateTime.MinValue;
                Task lastFrameWriteTask = null;

                while (!_stopCapturing.WaitOne(0) && _continueCapturing.WaitOne())
                {
                    var frame = _imageProvider.Capture();

                    var delay = lastFrameWriteTime == DateTime.MinValue ? 0
                        : (int)(DateTime.Now - lastFrameWriteTime).TotalMilliseconds;

                    lastFrameWriteTime = DateTime.Now;

                    lastFrameWriteTask = _videoEncoder.WriteFrameAsync(frame, delay);
                }

                // Wait for the last frame is written
                lastFrameWriteTask?.Wait();
            }
            catch (Exception e)
            {
                Stop();
                RaiseRecordingStopped(e);
            }
        }
Example #2
0
        void DoRecord()
        {
            try
            {
                var frameInterval = TimeSpan.FromSeconds(1.0 / _frameRate);

                while (_continueCapturing.WaitOne() && !_frames.IsAddingCompleted)
                {
                    var timestamp = DateTime.Now;

                    try { _frames.Add(_imageProvider.Capture()); }
                    catch { }

                    var timeTillNextFrame = timestamp + frameInterval - DateTime.Now;

                    if (timeTillNextFrame > TimeSpan.Zero)
                    {
                        Thread.Sleep(timeTillNextFrame);
                    }
                }
            }
            catch (Exception E)
            {
                ErrorOccured?.Invoke(E);

                Dispose(false, true);
            }
        }
Example #3
0
        private void Record()
        {
            try
            {
                IBitmapFrame lastFrame = null;

                while (!_stopCapturing.WaitOne(0) && _continueCapturing.WaitOne())
                {
                    var frame = _imageProvider.Capture();

                    var delay = (int)_timing.Elapsed.TotalMilliseconds;

                    _timing.Stop();
                    _timing.Start();

                    // delay is the time between this and next frame
                    if (lastFrame != null)
                    {
                        _videoEncoder.WriteFrame(lastFrame, delay);
                    }

                    lastFrame = frame.GenerateFrame();
                }
            }
            catch (Exception e)
            {
                ErrorOccurred?.Invoke(e);

                Dispose(true);
            }
        }
Example #4
0
        /// <summary>
        /// Capture an image.
        /// </summary>
        public Bitmap Capture()
        {
            var img = _sourceImageProvider.Capture();

            img.RotateFlip(_rotateFlipType);

            return(img);
        }
Example #5
0
        void DoRecord()
        {
            try
            {
                var frameInterval = TimeSpan.FromSeconds(1.0 / _frameRate);
                var frameCount    = 0;

                while (_continueCapturing.WaitOne() && !_frames.IsAddingCompleted)
                {
                    var timestamp = DateTime.Now;

                    var frame = _imageProvider.Capture();

                    try
                    {
                        _frames.Add(frame);

                        ++frameCount;
                    }
                    catch (InvalidOperationException)
                    {
                        return;
                    }

                    var requiredFrames = _sw.Elapsed.TotalSeconds * _frameRate;
                    var diff           = requiredFrames - frameCount;

                    for (var i = 0; i < diff; ++i)
                    {
                        try
                        {
                            _frames.Add(frame);

                            ++frameCount;
                        }
                        catch (InvalidOperationException)
                        {
                            return;
                        }
                    }

                    var timeTillNextFrame = timestamp + frameInterval - DateTime.Now;

                    if (timeTillNextFrame > TimeSpan.Zero)
                    {
                        Thread.Sleep(timeTillNextFrame);
                    }
                }
            }
            catch (Exception E)
            {
                ErrorOccured?.Invoke(E);

                Dispose(false, true);
            }
        }
Example #6
0
        public Bitmap Capture()
        {
            var bmp = _imageSource.Capture();

            #region Resize
            if (Settings.Instance.DoResize)
            {
                var nonResizedBmp = bmp;

                bmp = new Bitmap(Settings.Instance.ResizeWidth, Settings.Instance.ResizeHeight);

                using (var g = Graphics.FromImage(bmp))
                {
                    g.CompositingQuality = CompositingQuality.HighQuality;
                    g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                    g.SmoothingMode      = SmoothingMode.HighQuality;

                    if (_backgroundColor != Color.Transparent)
                    {
                        g.FillRectangle(new SolidBrush(_backgroundColor), 0, 0, Settings.Instance.ResizeWidth, Settings.Instance.ResizeHeight);
                    }

                    using (nonResizedBmp)
                        g.DrawImage(nonResizedBmp, 0, 0, _resizeWidth, _resizeHeight);
                }
            }
            #endregion

            #region Rotate Flip
            var flip = "Flip";

            if (!Settings.Instance.FlipHorizontal && !Settings.Instance.FlipVertical)
            {
                flip += "None";
            }

            if (Settings.Instance.FlipHorizontal)
            {
                flip += "X";
            }

            if (Settings.Instance.FlipVertical)
            {
                flip += "Y";
            }

            var rotateFlip = (RotateFlipType)Enum.Parse(typeof(RotateFlipType), Settings.Instance.RotateBy.ToString() + flip);

            bmp.RotateFlip(rotateFlip);
            #endregion

            return(bmp);
        }
Example #7
0
        void Record()
        {
            try
            {
                var  frameInterval     = TimeSpan.FromSeconds(1 / (double)_videoEncoder.FrameRate);
                Task frameWriteTask    = null;
                var  timeTillNextFrame = TimeSpan.Zero;

                while (!_stopCapturing.WaitOne(timeTillNextFrame) &&
                       _continueCapturing.WaitOne())
                {
                    var timestamp = DateTime.Now;

                    var frame = _imageProvider.Capture();

                    // Wait for the previous frame is written
                    if (frameWriteTask != null)
                    {
                        frameWriteTask.Wait();
                        _videoFrameWritten.Set();
                    }

                    if (_audioProvider != null &&
                        _audioProvider.IsSynchronizable)
                    {
                        if (WaitHandle.WaitAny(new WaitHandle[] { _audioBlockWritten, _stopCapturing }) == 1)
                        {
                            break;
                        }
                    }

                    // Start asynchronous (encoding and) writing of the new frame
                    frameWriteTask = _videoEncoder.WriteFrameAsync(frame);

                    timeTillNextFrame = timestamp + frameInterval - DateTime.Now;
                    if (timeTillNextFrame < TimeSpan.Zero)
                    {
                        timeTillNextFrame = TimeSpan.Zero;
                    }
                }

                // Wait for the last frame is written
                frameWriteTask?.Wait();
            }
            catch (Exception e)
            {
                Stop();

                RaiseRecordingStopped(e);
            }
        }
Example #8
0
        void DoRecord(IObservable <IRecordStep> StepsObservable, IObservable <Unit> ShotObservable)
        {
            var frames = ShotObservable.Select(M => _imageProvider.Capture())
                         .Zip(StepsObservable, (Frame, Step) =>
            {
                Step.Draw(Frame, _imageProvider.PointTransform);

                return(Frame.GenerateFrame(TimeSpan.Zero));
            });

            foreach (var frame in frames.ToEnumerable())
            {
                _videoWriter.WriteFrame(frame);
            }
        }
Example #9
0
        /// <inheritdoc />
        public IEditableFrame Capture()
        {
            var bmp = _imageProvider.Capture();

            // Overlays should have already been drawn on previous frame
            if (bmp is RepeatFrame)
            {
                return(bmp);
            }

            foreach (var overlay in _overlays)
            {
                overlay?.Draw(bmp, _imageProvider.PointTransform);
            }

            return(bmp);
        }
Example #10
0
        /// <summary>
        /// Captures an Image.
        /// </summary>
        public Bitmap Capture()
        {
            var bmp = _imageProvider.Capture();

            using (var g = Graphics.FromImage(bmp))
            {
                if (_overlays != null)
                {
                    foreach (var overlay in _overlays)
                    {
                        overlay?.Draw(g, _offset());
                    }
                }
            }

            return(bmp);
        }
        public IEditableFrame Capture()
        {
            var cursorPos = _platformServices.CursorPosition;

            var offsetRegion = new Rectangle(_regionAroundMouse.Location, _regionAroundMouse.Size);

            offsetRegion.Inflate(-_margin, -_margin);

            if (!offsetRegion.Contains(cursorPos))
            {
                ShiftRegion(cursorPos, offsetRegion);

                var screenBounds = _platformServices.DesktopRectangle;
                CheckBounds(screenBounds);
            }

            return(_regionProvider.Capture());
        }
Example #12
0
        void DoRecord()
        {
            var frameInterval = TimeSpan.FromSeconds(1.0 / _frameRate);

            while (_continueCapturing.WaitOne() && !_frames.IsAddingCompleted)
            {
                var timestamp = DateTime.Now;

                try { _frames.Add(_imageProvider.Capture()); }
                catch { }

                var timeTillNextFrame = timestamp + frameInterval - DateTime.Now;

                if (timeTillNextFrame > TimeSpan.Zero)
                {
                    Thread.Sleep(timeTillNextFrame);
                }
            }
        }
Example #13
0
        /// <inheritdoc />
        public IBitmapFrame Capture()
        {
            var bmp = _imageProvider.Capture();

            // Overlays should have already been drawn on previous frame
            if (bmp is RepeatFrame)
            {
                return(bmp);
            }

            using (var editor = bmp.GetEditor())
            {
                foreach (var overlay in _overlays)
                {
                    overlay?.Draw(editor.Graphics, _transform);
                }
            }

            return(bmp);
        }
Example #14
0
        void Record()
        {
            try
            {
                var lastFrameWriteTime = DateTime.MinValue;

                while (!_stopCapturing.WaitOne(0) && _continueCapturing.WaitOne())
                {
                    var frame = _imageProvider.Capture();

                    var delay = lastFrameWriteTime == DateTime.MinValue ? 0
                        : (int)(DateTime.Now - lastFrameWriteTime).TotalMilliseconds;

                    lastFrameWriteTime = DateTime.Now;

                    _videoEncoder.WriteFrame(frame, delay);
                }
            }
            catch { Dispose(); }
        }
Example #15
0
        /// <summary>
        /// Capture an image.
        /// </summary>
        public Bitmap Capture()
        {
            var bmp = _imageSource.Capture();

            var resizedBmp = new Bitmap(Width, Height);

            using (var g = Graphics.FromImage(resizedBmp))
            {
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                g.SmoothingMode      = SmoothingMode.HighQuality;

                if (_backgroundColor != Color.Transparent)
                {
                    g.FillRectangle(new SolidBrush(_backgroundColor), 0, 0, Width, Height);
                }

                g.DrawImage(bmp, 0, 0, _resizeWidth, _resizeHeight);
            }

            return(resizedBmp);
        }
Example #16
0
        /// <summary>
        /// Captures an Image.
        /// </summary>
        public IBitmapFrame Capture()
        {
            var bmp = _imageProvider.Capture();

            if (bmp is RepeatFrame)
            {
                return(bmp);
            }

            using (var editor = bmp.GetEditor())

            {
                if (_overlays != null)
                {
                    foreach (var overlay in _overlays)
                    {
                        overlay?.Draw(editor.Graphics, _transform);
                    }
                }
            }

            return(bmp);
        }
Example #17
0
        bool FrameWriter(TimeSpan Timestamp)
        {
            var editableFrame = _imageProvider.Capture();

            var frame = editableFrame.GenerateFrame(Timestamp);

            var success = AddFrame(frame);

            if (!success)
            {
                return(false);
            }

            try
            {
                _audioWriteTask = Task.Run(WriteAudio);

                return(true);
            }
            catch (InvalidOperationException)
            {
                return(false);
            }
        }
Example #18
0
        private async Task DoRecord()
        {
            try
            {
                var frameInterval = TimeSpan.FromSeconds(1.0 / _frameRate);
                var frameCount    = 0;

                // Returns false when stopped
                bool AddFrame(IBitmapFrame frame)
                {
                    try
                    {
                        _videoWriter.WriteFrame(frame);

                        ++frameCount;

                        return(true);
                    }
                    catch (InvalidOperationException)
                    {
                        return(false);
                    }
                }

                bool CanContinue()
                {
                    try
                    {
                        return(_continueCapturing.WaitOne());
                    }
                    catch (ObjectDisposedException)
                    {
                        return(false);
                    }
                }

                while (CanContinue() && !_cancellationToken.IsCancellationRequested)
                {
                    var timestamp = DateTime.Now;

                    if (_task != null)
                    {
                        // If false, stop recording
                        if (!await _task)
                        {
                            return;
                        }

                        var requiredFrames = _sw.Elapsed.TotalSeconds * _frameRate;
                        var diff           = requiredFrames - frameCount;

                        for (var i = 0; i < diff; ++i)
                        {
                            if (!AddFrame(RepeatFrame.Instance))
                            {
                                return;
                            }
                        }
                    }

                    _task = Task.Factory.StartNew(() =>
                    {
                        var editableFrame = _imageProvider.Capture();

                        if (_cancellationToken.IsCancellationRequested)
                        {
                            return(false);
                        }

                        var frame = editableFrame.GenerateFrame();

                        if (_cancellationToken.IsCancellationRequested)
                        {
                            return(false);
                        }

                        return(AddFrame(frame));
                    });

                    var timeTillNextFrame = timestamp + frameInterval - DateTime.Now;

                    if (timeTillNextFrame > TimeSpan.Zero)
                    {
                        Thread.Sleep(timeTillNextFrame);
                    }
                }
            }
            catch (Exception e)
            {
                lock (_syncLock)
                {
                    if (!_disposed)
                    {
                        ErrorOccurred?.Invoke(e);

                        Dispose(false);
                    }
                }
            }
        }
Example #19
0
        async Task DoRecord()
        {
            try
            {
                var frameInterval = TimeSpan.FromSeconds(1.0 / _frameRate);
                var frameCount    = 0;

                Task <IBitmapFrame> task = null;

                // Returns false when stopped
                bool AddFrame(IBitmapFrame Frame)
                {
                    try
                    {
                        _frames.Add(Frame);

                        ++frameCount;

                        return(true);
                    }
                    catch (InvalidOperationException)
                    {
                        return(false);
                    }
                }

                bool CanContinue()
                {
                    try
                    {
                        return(_continueCapturing.WaitOne());
                    }
                    catch (ObjectDisposedException)
                    {
                        return(false);
                    }
                }

                while (CanContinue() && !_frames.IsAddingCompleted)
                {
                    var timestamp = DateTime.Now;

                    if (task != null)
                    {
                        var frame = await task;

                        if (!AddFrame(frame))
                        {
                            return;
                        }

                        var requiredFrames = _sw.Elapsed.TotalSeconds * _frameRate;
                        var diff           = requiredFrames - frameCount;

                        for (var i = 0; i < diff; ++i)
                        {
                            if (!AddFrame(RepeatFrame.Instance))
                            {
                                return;
                            }
                        }
                    }

                    task = Task.Factory.StartNew(() => _imageProvider.Capture());

                    var timeTillNextFrame = timestamp + frameInterval - DateTime.Now;

                    if (timeTillNextFrame > TimeSpan.Zero)
                    {
                        Thread.Sleep(timeTillNextFrame);
                    }
                }
            }
            catch (Exception E)
            {
                ErrorOccured?.Invoke(E);

                Dispose(false, true);
            }
        }
Example #20
0
        async Task DoRecord()
        {
            try
            {
                var frameInterval = TimeSpan.FromSeconds(1.0 / _frameRate);
                var frameCount    = 0;

                Task <bool> task = null;

                // Returns false when stopped
                bool AddFrame(IBitmapFrame Frame)
                {
                    try
                    {
                        _frames.Add(Frame);

                        ++frameCount;

                        return(true);
                    }
                    catch (InvalidOperationException)
                    {
                        return(false);
                    }
                }

                bool CanContinue()
                {
                    try
                    {
                        return(_continueCapturing.WaitOne());
                    }
                    catch (ObjectDisposedException)
                    {
                        return(false);
                    }
                }

                while (CanContinue() && !_frames.IsAddingCompleted)
                {
                    if (!_congestion && _frames.Count > _congestionFrameCount)
                    {
                        _congestion = true;

                        Console.WriteLine("Congestion: ON");
                    }
                    else if (_congestion && _frames.Count < _congestionFrameCount / 2)
                    {
                        _congestion = false;

                        Console.WriteLine("Congestion: OFF");
                    }

                    if (_frames.Count > _maxFrameCount)
                    {
                        throw new Exception(@"System can't keep up with the Recording. Frames are not being written. Retry again or try with a smaller region, lower Frame Rate or another Codec.");
                    }

                    var timestamp = DateTime.Now;

                    if (task != null)
                    {
                        // If false, stop recording
                        if (!await task)
                        {
                            return;
                        }

                        if (!_congestion)
                        {
                            var requiredFrames = _sw.Elapsed.TotalSeconds * _frameRate;
                            var diff           = requiredFrames - frameCount;

                            for (var i = 0; i < diff; ++i)
                            {
                                if (!AddFrame(RepeatFrame.Instance))
                                {
                                    return;
                                }
                            }
                        }
                    }

                    task = Task.Factory
                           .StartNew(() => _imageProvider.Capture())
                           .ContinueWith(T => AddFrame(T.Result), TaskContinuationOptions.OnlyOnRanToCompletion);

                    var timeTillNextFrame = timestamp + frameInterval - DateTime.Now;

                    if (timeTillNextFrame > TimeSpan.Zero)
                    {
                        Thread.Sleep(timeTillNextFrame);
                    }
                }
            }
            catch (Exception e)
            {
                lock (_syncLock)
                {
                    if (!_disposed)
                    {
                        ErrorOccurred?.Invoke(e);

                        Dispose(false, true);
                    }
                }
            }
        }
Example #21
0
        /// <summary>
        /// Capture an image.
        /// </summary>
        public Bitmap Capture()
        {
            var bmp = _sourceImageProvider.Capture();

            return(bmp.Clone(_cropRectangle, bmp.PixelFormat));
        }