Esempio n. 1
0
 /// <summary>
 /// Captures a specific area and saves it to a file.
 /// </summary>
 public static void RectangleToFile(Shapes.Rectangle bounds, string filePath)
 {
     using (var bmp = Rectangle(bounds))
     {
         Logger.Default.Info($"Capture.RectangleToFile: {bounds} {filePath}");
         bmp.Save(filePath, ImageFormat.Png);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Captures the whole screen (all monitors).
        /// </summary>
        public static CaptureImage Screen()
        {
            var screen = new Shapes.Rectangle(
                SystemParameters.VirtualScreenLeft, SystemParameters.VirtualScreenTop,
                SystemParameters.VirtualScreenWidth, SystemParameters.VirtualScreenHeight);

            return(Rectangle(screen));
        }
Esempio n. 3
0
        /// <summary>
        /// Captures a specific area from the screen
        /// </summary>
        public static Bitmap CaptureArea(Shapes.Rectangle rectangle)
        {
            var width    = rectangle.Width.ToInt();
            var height   = rectangle.Height.ToInt();
            var bmp      = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var graphics = Graphics.FromImage(bmp);

            graphics.CopyFromScreen(rectangle.Left.ToInt(), rectangle.Top.ToInt(), 0, 0, new System.Drawing.Size(width, height), CopyPixelOperation.SourceCopy);
            return(bmp);
        }
Esempio n. 4
0
        /// <summary>
        /// Captures a rectangle inside an element and returns the image.
        /// </summary>
        public static CaptureImage ElementRectangle(AutomationElement element, Shapes.Rectangle rectangle)
        {
            var elementBounds = element.BoundingRectangle;
            // Calculate the rectangle that should be captured
            var capturingRectangle = new Shapes.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));
        }
Esempio n. 5
0
        /// <summary>
        /// Captures a specific area from the screen.
        /// </summary>
        public static Bitmap Rectangle(Shapes.Rectangle bounds)
        {
            var width  = bounds.Width.ToInt();
            var height = bounds.Height.ToInt();
            var bmp    = new Bitmap(width, height, PixelFormat.Format32bppArgb);

            using (var graphics = Graphics.FromImage(bmp))
            {
                using (var screen = Screen())
                {
                    graphics.DrawImage(
                        screen,
                        new Rectangle(0, 0, width, height),
                        new Rectangle((int)bounds.X, (int)bounds.Y, (int)bounds.Width, (int)bounds.Height),
                        GraphicsUnit.Pixel);
                }
            }
            return(bmp);
        }
Esempio n. 6
0
        /// <summary>
        /// Captures a specific area from the screen.
        /// </summary>
        public static CaptureImage Rectangle(Shapes.Rectangle bounds)
        {
            // Use P/Invoke because of: https://stackoverflow.com/a/3072580/1069200
            var sz      = new System.Drawing.Size((int)bounds.Width, (int)bounds.Height);
            var hDesk   = GetDesktopWindow();
            var hSrce   = GetWindowDC(hDesk);
            var hDest   = CreateCompatibleDC(hSrce);
            var hBmp    = CreateCompatibleBitmap(hSrce, sz.Width, sz.Height);
            var hOldBmp = SelectObject(hDest, hBmp);

            BitBlt(hDest, 0, 0, sz.Width, sz.Height, hSrce, (int)bounds.X, (int)bounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
            var bmp = Image.FromHbitmap(hBmp);

            SelectObject(hDest, hOldBmp);
            DeleteObject(hBmp);
            DeleteDC(hDest);
            ReleaseDC(hDesk, hSrce);
            return(new CaptureImage(bmp));
        }
Esempio n. 7
0
 /// <summary>
 /// Captures a specific area in a WPF friendly format.
 /// </summary>
 public static BitmapImage RectangleWpf(Shapes.Rectangle bounds)
 {
     return(Rectangle(bounds).ToWpf());
 }
Esempio n. 8
0
        /// <summary>
        /// Captures a specific area and saves it to a file
        /// </summary>
        public static void CaptureAreaToFile(Shapes.Rectangle rectangle, string filePath)
        {
            var bmp = CaptureArea(rectangle);

            bmp.Save(filePath, ImageFormat.Png);
        }
Esempio n. 9
0
 public static BitmapImage CaptureAreaWpf(Shapes.Rectangle rectangle)
 {
     return(CaptureArea(rectangle).ToWpf());
 }