/// <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(); }
/// <summary> /// Captures all screens an element is on. /// </summary> public static CaptureImage ScreensWithElement(AutomationElement element, CaptureSettings settings = null) { var elementRectangle = element.BoundingRectangle; var intersectedScreenBounds = new List <Rectangle>(); // Calculate which screens intersect with the element for (var screenIndex = 0; screenIndex < User32.GetSystemMetrics(SystemMetric.SM_CMONITORS); screenIndex++) { var screenRectangle = GetBoundsByScreenIndex(screenIndex); if (screenRectangle.IntersectsWith(elementRectangle)) { intersectedScreenBounds.Add(screenRectangle); } } if (intersectedScreenBounds.Count > 0) { var minX = intersectedScreenBounds.Min(x => x.Left); var maxX = intersectedScreenBounds.Max(x => x.Right); var minY = intersectedScreenBounds.Min(x => x.Top); var maxY = intersectedScreenBounds.Max(x => x.Bottom); var captureRect = new Rectangle(minX, minY, maxX - minX, maxY - minY); return(Rectangle(captureRect, settings)); } // Fallback to the whole screen return(Screen()); }
/// <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)); }
/// <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; _bitmapImageLazy = new Lazy <BitmapImage>(ToWpf); }
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)); }
/// <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)); }
/// <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)); }
/// <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 < System.Windows.Forms.Screen.AllScreens.Length) { var rectangle = System.Windows.Forms.Screen.AllScreens[screenIndex].Bounds; capturingRectangle = rectangle; } else { // Use the entire desktop capturingRectangle = new Rectangle( SystemParameters.VirtualScreenLeft.ToInt(), SystemParameters.VirtualScreenTop.ToInt(), SystemParameters.VirtualScreenWidth.ToInt(), SystemParameters.VirtualScreenHeight.ToInt()); } return(Rectangle(capturingRectangle, settings)); }
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); }
/// <summary> /// Captures a rectangle inside an element and returns the image. /// </summary> public static CaptureImage ElementRectangle(AutomationElement 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 FlaUIException($"The given rectangle ({capturingRectangle}) is out of bounds of the element ({elementBounds})."); } return(Rectangle(capturingRectangle, settings)); }
/// <summary> /// Captures an element and returns the image. /// </summary> public static CaptureImage Element(AutomationElement element, CaptureSettings settings = null) { return(Rectangle(element.BoundingRectangle, settings)); }
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); }
public static Point ScaleAccordingToSettings(int x, int y, Rectangle originalBounds, CaptureSettings captureSettings) { return(ScaleAccordingToSettings(new Point(x, y), originalBounds, captureSettings)); }
/// <summary> /// Captures the main (primary) screen. /// </summary> public static CaptureImage MainScreen(CaptureSettings settings = null) { return(Rectangle(System.Windows.Forms.Screen.PrimaryScreen.Bounds, settings)); }