Пример #1
0
        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.");
                }
            }
        }
Пример #2
0
        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.");
                    }
                }
            }
        }
Пример #3
0
 /// <summary>
 /// Returns true if screenshots can be captured.
 /// </summary>
 /// <remarks>
 /// <para>
 /// This method may return false if the application is running as a service which has
 /// not been granted the right to interact with the desktop.
 /// </para>
 /// </remarks>
 /// <returns>True if the screen can be captured.</returns>
 public static bool CanCaptureScreenshot()
 {
     return(ScreenGrabber.CanCaptureScreenshot());
 }