Ejemplo n.º 1
0
        /// <summary>
        /// Captures a specific area from the screen.
        /// </summary>
        public static CaptureImage Rectangle(Rectangle bounds, CaptureSettings settings = null)
        {
            // Calculate the size of the output rectangle
            var outputRectangle = CaptureUtilities.ScaleAccordingToSettings(bounds, settings);

            Bitmap bmp;

            if (outputRectangle.Width == bounds.Width || outputRectangle.Height == bounds.Height)
            {
                // Capture directly without any resizing
                bmp = CaptureDesktopToBitmap(bounds.Width, bounds.Height, (dest, src) =>
                {
                    Gdi32.BitBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
                });
            }
            else
            {
                //  Capture with scaling
                bmp = CaptureDesktopToBitmap(outputRectangle.Width, outputRectangle.Height, (dest, src) =>
                {
                    Gdi32.SetStretchBltMode(dest, StretchMode.STRETCH_HALFTONE);
                    Gdi32.StretchBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, bounds.Width, bounds.Height, TernaryRasterOperations.SRCCOPY | TernaryRasterOperations.CAPTUREBLT);
                });
            }
            return(new CaptureImage(bmp, bounds, settings));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Scales a rectangle according to the given settings.
        /// </summary>
        /// <param name="originalBounds">The original bounds of the captured image.</param>
        /// <param name="captureSettings">The settings to use for the capture.</param>
        /// <returns>The transformed rectangle.</returns>
        public static Rectangle ScaleAccordingToSettings(Rectangle originalBounds, CaptureSettings captureSettings)
        {
            // Default is the original size
            var outputWidth  = originalBounds.Width;
            var outputHeight = originalBounds.Height;

            if (captureSettings != null)
            {
                if (captureSettings.OutputScale != 1)
                {
                    // Calculate with the scale
                    outputWidth  = (originalBounds.Width * captureSettings.OutputScale).ToInt();
                    outputHeight = (originalBounds.Height * captureSettings.OutputScale).ToInt();
                }
                else if (captureSettings.OutputHeight == -1 && captureSettings.OutputWidth != -1)
                {
                    // Adjust the height
                    outputWidth = captureSettings.OutputWidth;
                    var percent = outputWidth / (double)originalBounds.Width;
                    outputHeight = (originalBounds.Height * percent).ToInt();
                }
                else if (captureSettings.OutputHeight != -1 && captureSettings.OutputWidth == -1)
                {
                    // Adjust the width
                    outputHeight = captureSettings.OutputHeight;
                    var percent = outputHeight / (double)originalBounds.Height;
                    outputWidth = (originalBounds.Width * percent).ToInt();
                }
            }
            return(new Rectangle(0, 0, outputWidth, outputHeight));
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Creates a <see cref="CaptureImage"/> object with the given <see cref="Bitmap"/>.
 /// </summary>
 public CaptureImage(Bitmap bitmap, Rectangle originalBounds, CaptureSettings settings)
 {
     Bitmap         = bitmap;
     OriginalBounds = originalBounds;
     Settings       = settings;
     OnInitialized();
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Captures the main (primary) screen.
        /// </summary>
        public static CaptureImage MainScreen(CaptureSettings settings = null)
        {
            var primaryScreenBounds = new Rectangle(
                0, 0,
                User32.GetSystemMetrics(SystemMetric.SM_CXSCREEN), User32.GetSystemMetrics(SystemMetric.SM_CYSCREEN));

            return(Rectangle(primaryScreenBounds, settings));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Captures the whole screen (all monitors).
        /// </summary>
        public static CaptureImage Screen(int screenIndex = -1, CaptureSettings settings = null)
        {
            Rectangle capturingRectangle;

            // Take the appropriate screen if requested
            if (screenIndex >= 0 && screenIndex < User32.GetSystemMetrics(SystemMetric.SM_CMONITORS))
            {
                var rectangle = GetBoundsByScreenIndex(screenIndex);
                capturingRectangle = rectangle;
            }
            else
            {
                // Use the entire desktop
                capturingRectangle = new Rectangle(
                    User32.GetSystemMetrics(SystemMetric.SM_XVIRTUALSCREEN), User32.GetSystemMetrics(SystemMetric.SM_YVIRTUALSCREEN),
                    User32.GetSystemMetrics(SystemMetric.SM_CXVIRTUALSCREEN), User32.GetSystemMetrics(SystemMetric.SM_CYVIRTUALSCREEN));
            }
            return(Rectangle(capturingRectangle, settings));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Calculates a scale factor according to the bounds and capture settings.
        /// </summary>
        /// <param name="originalBounds">The original bounds of the captured image.</param>
        /// <param name="captureSettings">The settings to use for the capture.</param>
        /// <returns>A scale factor, defaults to 1 which means original size.</returns>
        public static double GetScale(Rectangle originalBounds, CaptureSettings captureSettings)
        {
            double scale = 1;

            if (captureSettings != null)
            {
                scale = captureSettings.OutputScale;
                if (scale == 1)
                {
                    if (captureSettings.OutputHeight == -1 && captureSettings.OutputWidth != -1)
                    {
                        // Calculate the scale by a defined width
                        scale = captureSettings.OutputWidth / (double)originalBounds.Width;
                    }
                    else if (captureSettings.OutputHeight != -1 && captureSettings.OutputWidth == -1)
                    {
                        // Calculate the scale by a defined height
                        scale = captureSettings.OutputHeight / (double)originalBounds.Height;
                    }
                }
            }
            return(scale);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Captures a rectangle inside an element and returns the image.
        /// </summary>
        public static CaptureImage ElementRectangle(SHAutomationElement element, Rectangle rectangle, CaptureSettings settings = null)
        {
            var elementBounds = element.BoundingRectangle;
            // Calculate the rectangle that should be captured
            var capturingRectangle = new Rectangle(elementBounds.Left + rectangle.Left, elementBounds.Top + rectangle.Top, rectangle.Width, rectangle.Height);

            // Check if the element contains the rectangle that should be captured
            if (!elementBounds.Contains(capturingRectangle))
            {
                throw new SHAutomationException($"The given rectangle ({capturingRectangle}) is out of bounds of the element ({elementBounds}).");
            }
            return(Rectangle(capturingRectangle, settings));
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Captures an element and returns the image.
 /// </summary>
 public static CaptureImage Element(SHAutomationElement element, CaptureSettings settings = null)
 {
     return(Rectangle(element.BoundingRectangle, settings));
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Scales a point according to the given settings.
        /// </summary>
        /// <param name="point">The point to scale.</param>
        /// <param name="originalBounds">The original bounds of the captured image.</param>
        /// <param name="captureSettings">The settings to use for the capture.</param>
        /// <returns>The transformed point.</returns>
        public static Point ScaleAccordingToSettings(Point point, Rectangle originalBounds, CaptureSettings captureSettings)
        {
            var scale = GetScale(originalBounds, captureSettings);

            return(scale != 1 ? new Point((point.X * scale).ToInt(), (point.Y * scale).ToInt()) : point);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Scales a point according to the given settings.
 /// </summary>
 /// <param name="x">The x-position of the point to scale.</param>
 /// <param name="y">The y-position of the pint to scale.</param>
 /// <param name="originalBounds">The original bounds of the captured image.</param>
 /// <param name="captureSettings">The settings to use for the capture.</param>
 /// <returns>The transformed point.</returns>
 public static Point ScaleAccordingToSettings(int x, int y, Rectangle originalBounds, CaptureSettings captureSettings)
 {
     return(ScaleAccordingToSettings(new Point(x, y), originalBounds, captureSettings));
 }