public void Parameters_ReturnsParameters()
 {
     var parameters = new CaptureParameters();
     using (var grabber = new ScreenGrabber(parameters))
     {
         Assert.AreSame(parameters, grabber.Parameters);
     }
 }
 public void ScreenshotWidth_ReturnsScaledWidth()
 {
     var parameters = new CaptureParameters() { Zoom = 0.25 };
     using (var grabber = new ScreenGrabber(parameters))
     {
         Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Width / 2, grabber.ScreenshotWidth, 1);
     }
 }
        public void ScreenshotWidth_WhenDisposed_Throws()
        {
            var parameters = new CaptureParameters();
            var grabber = new ScreenGrabber(parameters);
            grabber.Dispose();

            int x;
            Assert.Throws<ObjectDisposedException>(() => x = grabber.ScreenshotWidth);
        }
 public void Grabber_ReturnsGrabber()
 {
     var grabber = new ScreenGrabber(new CaptureParameters());
     var video = new FlashScreenVideo(new FlashScreenVideoParameters(grabber.ScreenshotWidth, grabber.ScreenshotHeight, 5));
     using (var recorder = new ScreenRecorder(grabber, video))
     {
         Assert.AreSame(grabber, recorder.Grabber);
     }
 }
        public void Grabber_WhenDisposed_Throws()
        {
            var grabber = new ScreenGrabber(new CaptureParameters());
            var video = new FlashScreenVideo(new FlashScreenVideoParameters(grabber.ScreenshotWidth, grabber.ScreenshotHeight, 5));
            var recorder = new ScreenRecorder(grabber, video);
            recorder.Dispose();

            ScreenGrabber x;
            Assert.Throws<ObjectDisposedException>(() => x = recorder.Grabber);
        }
Beispiel #6
0
        /// <summary>
        /// Captures an image of the entire desktop.
        /// </summary>
        /// <param name="parameters">The capture parameters.</param>
        /// <returns>The screenshot.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        /// <exception cref="ScreenshotNotAvailableException">Thrown if a screenshot cannot be captured at this time.</exception>
        public static Bitmap Screenshot(CaptureParameters parameters)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            using (var grabber = new ScreenGrabber(parameters))
            {
                grabber.OverlayManager.AddOverlay(GetOverlayManager().ToOverlay());
                return grabber.CaptureScreenshot(null);
            }
        }
        public void Constructor_WhenVideoSizeDoesNotMatchGrabberScreenshotsSize_Throws()
        {
            var grabber = new ScreenGrabber(new CaptureParameters());

            var video = new FlashScreenVideo(new FlashScreenVideoParameters(1, grabber.ScreenshotHeight, 5));
            var ex = Assert.Throws<ArgumentException>(() => new ScreenRecorder(grabber, video));
            Assert.Contains(ex.Message, "The video dimensions must be exactly the same as the screenshots obtained by the grabber.");

            video = new FlashScreenVideo(new FlashScreenVideoParameters(grabber.ScreenshotWidth, 1, 5));
            ex = Assert.Throws<ArgumentException>(() => new ScreenRecorder(grabber, video));
            Assert.Contains(ex.Message, "The video dimensions must be exactly the same as the screenshots obtained by the grabber.");
        }
        /// <summary>
        /// Creates a screen recorder.
        /// </summary>
        /// <param name="grabber">The screen grabber.</param>
        /// <param name="video">The video to which frames are to be added.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="grabber"/> or
        /// <paramref name="video"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="video"/> size
        /// is not exactly equal to the <paramref name="grabber"/> screenshot size.</exception>
        public ScreenRecorder(ScreenGrabber grabber, Video video)
        {
            if (grabber == null)
                throw new ArgumentNullException("grabber");
            if (video == null)
                throw new ArgumentNullException("video");
            if (video.Parameters.Width != grabber.ScreenshotWidth ||
                video.Parameters.Height != grabber.ScreenshotHeight)
                throw new ArgumentException("The video dimensions must be exactly the same as the screenshots obtained by the grabber.");

            this.grabber = grabber;
            this.video = video;

            overlayManager = new OverlayManager();
        }
Beispiel #9
0
        /// <summary>
        /// Disposes the screen recorder.
        /// </summary>
        /// <param name="disposing">True if <see cref="Dispose()"/> was called directly.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                Stop(false);

                if (grabber != null)
                {
                    grabber.Dispose();
                }
            }

            grabber    = null;
            lastBitmap = null;
            video      = null;
        }
Beispiel #10
0
        /// <summary>
        /// Creates a screen recorder.
        /// </summary>
        /// <param name="grabber">The screen grabber.</param>
        /// <param name="video">The video to which frames are to be added.</param>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="grabber"/> or
        /// <paramref name="video"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown if <paramref name="video"/> size
        /// is not exactly equal to the <paramref name="grabber"/> screenshot size.</exception>
        public ScreenRecorder(ScreenGrabber grabber, Video video)
        {
            if (grabber == null)
            {
                throw new ArgumentNullException("grabber");
            }
            if (video == null)
            {
                throw new ArgumentNullException("video");
            }
            if (video.Parameters.Width != grabber.ScreenshotWidth ||
                video.Parameters.Height != grabber.ScreenshotHeight)
            {
                throw new ArgumentException("The video dimensions must be exactly the same as the screenshots obtained by the grabber.");
            }

            this.grabber = grabber;
            this.video   = video;

            overlayManager = new OverlayManager();
        }
        /// <summary>
        /// Disposes the screen recorder.
        /// </summary>
        /// <param name="disposing">True if <see cref="Dispose()"/> was called directly.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                Stop(false);

                if (grabber != null)
                    grabber.Dispose();
            }

            grabber = null;
            lastBitmap = null;
            video = null;
        }
        public void CaptureScreenshot_WhenDisposed_Throws()
        {
            var parameters = new CaptureParameters();
            var grabber = new ScreenGrabber(parameters);
            grabber.Dispose();

            Assert.Throws<ObjectDisposedException>(() => grabber.CaptureScreenshot(null));
        }
        public void Constructor_WhenVideoIsNull_Throws()
        {
            var grabber = new ScreenGrabber(new CaptureParameters());

            Assert.Throws<ArgumentNullException>(() => new ScreenRecorder(grabber, null));
        }
        public void CaptureScreenshot_WhenBitmapIsNotNull_CapturesScreenshotIntoProvidedBitmap()
        {
            var parameters = new CaptureParameters() { Zoom = 0.25 };
            using (var grabber = new ScreenGrabber(parameters))
            {
                using (Bitmap bitmap = new Bitmap(grabber.ScreenshotWidth, grabber.ScreenshotHeight))
                {
                    if (ScreenGrabber.CanCaptureScreenshot())
                    {
                        Bitmap returnedBitmap = grabber.CaptureScreenshot(bitmap);
                        TestLog.EmbedImage("Screenshot with 0.25x zoom", bitmap);

                        Assert.AreSame(bitmap, returnedBitmap);
                    }
                    else
                    {
                        Assert.Throws<ScreenshotNotAvailableException>(() => grabber.CaptureScreenshot(bitmap),
                            "CanCaptureScreenshot returned false so expected an exception to be thrown.");
                    }
                }
            }
        }
        public void CaptureScreenshot_WhenBitmapIsNullAndZoomed_CapturesScaledScreenshotIntoNewBitmap()
        {
            var parameters = new CaptureParameters() { Zoom = 0.25 };
            using (var grabber = new ScreenGrabber(parameters))
            {
                if (ScreenGrabber.CanCaptureScreenshot())
                {
                    using (Bitmap bitmap = grabber.CaptureScreenshot(null))
                    {
                        TestLog.EmbedImage("Screenshot with 0.25x zoom", bitmap);

                        Assert.Multiple(() =>
                        {
                            Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Width / 2,
                                grabber.ScreenshotWidth, 1);
                            Assert.AreApproximatelyEqual(ScreenGrabber.GetScreenSize().Height / 2,
                                grabber.ScreenshotHeight, 1);
                            Assert.AreEqual(grabber.ScreenshotWidth, bitmap.Width);
                            Assert.AreEqual(grabber.ScreenshotHeight, bitmap.Height);
                        });
                    }
                }
                else
                {
                    Assert.Throws<ScreenshotNotAvailableException>(() => grabber.CaptureScreenshot(null),
                        "CanCaptureScreenshot returned false so expected an exception to be thrown.");
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// Starts recording a screen capture video of the entire desktop.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Recording a screen capture video can be very CPU and space intensive particularly
        /// when running tests on a single-core CPU.  We recommend calling
        /// <see cref="StartRecording(CaptureParameters, double)" /> with
        /// a <see cref="CaptureParameters.Zoom" /> factor of 0.25 or less and a frame rate
        /// of no more than 5 to 10 frames per second.
        /// </para>
        /// </remarks>
        /// <param name="parameters">The capture parameters.</param>
        /// <param name="framesPerSecond">The number of frames per second to capture.</param>
        /// <returns>The recorder.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="parameters"/> is null.</exception>
        /// <exception cref="ScreenshotNotAvailableException">Thrown if a screenshot cannot be captured at this time.</exception>
        public static ScreenRecorder StartRecording(CaptureParameters parameters, double framesPerSecond)
        {
            if (parameters == null)
                throw new ArgumentNullException("parameters");

            ScreenGrabber.ThrowIfScreenshotNotAvailable();

            ScreenGrabber grabber = new ScreenGrabber(parameters);
            try
            {
                FlashScreenVideo video = new FlashScreenVideo(new FlashScreenVideoParameters(
                    grabber.ScreenshotWidth, grabber.ScreenshotHeight, framesPerSecond));

                ScreenRecorder recorder = new ScreenRecorder(grabber, video);
                try
                {
                    recorder.OverlayManager.AddOverlay(GetOverlayManager().ToOverlay());

                    recorder.Start();
                    return recorder;
                }
                catch
                {
                    recorder.Dispose();
                    throw;
                }
            }
            catch
            {
                grabber.Dispose();
                throw;
            }
        }
        public void CaptureScreenshot_WhenBitmapIsNotTheRightSize_Throws()
        {
            var parameters = new CaptureParameters() { Zoom = 0.25 };
            using (var grabber = new ScreenGrabber(parameters))
            {
                var ex = Assert.Throws<ArgumentException>(() => grabber.CaptureScreenshot(new Bitmap(1, grabber.ScreenshotHeight)));
                Assert.Contains(ex.Message, "The bitmap dimensions must exactly match the screenshot dimensions.");

                ex = Assert.Throws<ArgumentException>(() => grabber.CaptureScreenshot(new Bitmap(grabber.ScreenshotWidth, 1)));
                Assert.Contains(ex.Message, "The bitmap dimensions must exactly match the screenshot dimensions.");
            }
        }
        public void Start_CapturesVideoUntilStopped()
        {
            var grabber = new ScreenGrabber(new CaptureParameters() { Zoom = 0.25 });
            var video = new FlashScreenVideo(new FlashScreenVideoParameters(grabber.ScreenshotWidth, grabber.ScreenshotHeight, 5));
            using (var recorder = new ScreenRecorder(grabber, video))
            {
                recorder.Start();
                Thread.Sleep(2000);
                recorder.Stop();

                TestLog.EmbedVideo("Video", recorder.Video);
            }
        }