How to use Rectangle method of FlaUI.Core.Capturing.Capture class

Best FlaUI code snippet using FlaUI.Core.Capturing.Capture.Rectangle

Capture.cs

Source:Capture.cs Github

copy

Full Screen

...16 /// Captures the main (primary) screen.17 /// </summary>18 public static CaptureImage MainScreen(CaptureSettings settings = null)19 {20 return Rectangle(System.Windows.Forms.Screen.PrimaryScreen.Bounds, settings);21 }22 /// <summary>23 /// Captures the whole screen (all monitors).24 /// </summary>25 public static CaptureImage Screen(int screenIndex = -1, CaptureSettings settings = null)26 {27 Rectangle capturingRectangle;28 // Take the appropriate screen if requested29 if (screenIndex >= 0 && screenIndex < System.Windows.Forms.Screen.AllScreens.Length)30 {31 var rectangle = System.Windows.Forms.Screen.AllScreens[screenIndex].Bounds;32 capturingRectangle = rectangle;33 }34 else35 {36 // Use the entire desktop37 capturingRectangle = new Rectangle(38 SystemParameters.VirtualScreenLeft.ToInt(), SystemParameters.VirtualScreenTop.ToInt(),39 SystemParameters.VirtualScreenWidth.ToInt(), SystemParameters.VirtualScreenHeight.ToInt());40 }41 return Rectangle(capturingRectangle, settings);42 }43 /// <summary>44 /// Captures an element and returns the image.45 /// </summary>46 public static CaptureImage Element(AutomationElement element, CaptureSettings settings = null)47 {48 return Rectangle(element.Properties.BoundingRectangle.Value, settings);49 }50 /// <summary>51 /// Captures a rectangle inside an element and returns the image.52 /// </summary>53 public static CaptureImage ElementRectangle(AutomationElement element, Rectangle rectangle, CaptureSettings settings = null)54 {55 var elementBounds = element.BoundingRectangle;56 // Calculate the rectangle that should be captured57 var capturingRectangle = new Rectangle(elementBounds.Left + rectangle.Left, elementBounds.Top + rectangle.Top, rectangle.Width, rectangle.Height);58 // Check if the element contains the rectangle that should be captured59 if (!elementBounds.Contains(capturingRectangle))60 {61 throw new FlaUIException($"The given rectangle ({capturingRectangle}) is out of bounds of the element ({elementBounds}).");62 }63 return Rectangle(capturingRectangle, settings);64 }65 /// <summary>66 /// Captures a specific area from the screen.67 /// </summary>68 public static CaptureImage Rectangle(Rectangle bounds, CaptureSettings settings = null)69 {70 // Calculate the size of the output rectangle71 var outputRectangle = CaptureUtilities.ScaleAccordingToSettings(bounds, settings);72 Bitmap bmp;73 if (outputRectangle.Width == bounds.Width || outputRectangle.Height == bounds.Height)74 {75 // Capture directly without any resizing76 bmp = CaptureDesktopToBitmap(bounds.Width, bounds.Height, (dest, src) =>77 {78 Gdi32.BitBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);79 });80 }81 else82 {83 // Capture with scaling84 bmp = CaptureDesktopToBitmap(outputRectangle.Width, outputRectangle.Height, (dest, src) =>85 {86 Gdi32.SetStretchBltMode(dest, StretchMode.STRETCH_HALFTONE);87 Gdi32.StretchBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, bounds.Width, bounds.Height, TernaryRasterOperations.SRCCOPY | TernaryRasterOperations.CAPTUREBLT);88 });89 }90 return new CaptureImage(bmp, bounds, settings);91 }92 private static Bitmap CaptureDesktopToBitmap(int width, int height, Action<IntPtr, IntPtr> action)93 {94 // Use P/Invoke because of: https://stackoverflow.com/a/3072580/106920095 var hDesk = User32.GetDesktopWindow();96 var hSrc = User32.GetWindowDC(hDesk);97 var hDest = Gdi32.CreateCompatibleDC(hSrc);98 var hBmp = Gdi32.CreateCompatibleBitmap(hSrc, width, height);99 var hPrevBmp = Gdi32.SelectObject(hDest, hBmp);100 action(hDest, hSrc);101 var bmp = Image.FromHbitmap(hBmp);...

Full Screen

Full Screen

CaptureUtil.cs

Source:CaptureUtil.cs Github

copy

Full Screen

...4namespace FlaUI.Utilities5{6 public static class CaptureUtil7 {8 public static void CaptureToFile(string filePath, AutomationElement element = null, Rectangle? rectangle = null)9 {10 CaptureImage captureImage;11 if (element == null)12 {13 captureImage = Capture.Screen();14 }15 else if (rectangle == null)16 {17 captureImage = Capture.Element(element);18 }19 else20 {21 captureImage = Capture.ElementRectangle(element, (Rectangle)rectangle);22 }23 captureImage.ToFile(filePath);24 }25 public static void CaptureScreenToFile(this AutomationElement element, string filePath)26 {27 CaptureImage captureImage = Capture.Screen();28 captureImage.ToFile(filePath);29 }30 public static void CaptureElementToFile(this AutomationElement element, string filePath)31 {32 CaptureImage captureImage = Capture.Element(element);33 captureImage.ToFile(filePath);34 }35 public static void CaptureElementRectangleToFile(this AutomationElement element, Rectangle rectangle, string filePath)36 {37 CaptureImage captureImage = Capture.ElementRectangle(element, rectangle);38 captureImage.ToFile(filePath);39 }40 }41}...

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Linq;4using System.Text;5using System.Threading.Tasks;6using FlaUI.Core.Capturing;7using System.Drawing;8using System.Drawing.Imaging;9using System.IO;10{11 {12 static void Main(string[] args)13 {14 var rect = Capture.Rectangle();15 var bitmap = Capture.Rectangle(rect);16 var bitmapBytes = Capture.RectangleBytes(rect);17 bitmap.Save(@"C:\Users\Public\Pictures\Sample Pictures\Rectangle.png", ImageFormat.Png);18 bitmap.Dispose();19 Console.WriteLine("Rectangle image saved to disk");20 Console.ReadLine();21 }22 }23}24using System;25using System.Collections.Generic;26using System.Linq;27using System.Text;28using System.Threading.Tasks;29using FlaUI.Core.Capturing;30using System.Drawing;31using System.Drawing.Imaging;32using System.IO;33{34 {35 static void Main(string[] args)36 {37 var bitmap = Capture.Full();38 var bitmapBytes = Capture.FullBytes();39 bitmap.Save(@"C:\Users\Public\Pictures\Sample Pictures\Full.png", ImageFormat.Png);40 bitmap.Dispose();41 Console.WriteLine("Full image saved to disk");42 Console.ReadLine();43 }44 }45}46using System;47using System.Collections.Generic;48using System.Linq;49using System.Text;50using System.Threading.Tasks;51using FlaUI.Core.Capturing;52using System.Drawing;53using System.Drawing.Imaging;54using System.IO;55using FlaUI.Core.AutomationElements;56{57 {58 static void Main(string[] args)59 {60 var window = Application.Launch("notepad.exe").GetMainWindow(Automation);61 var rect = window.BoundingRectangle;62 var bitmap = Capture.Window(window);63 var bitmapBytes = Capture.WindowBytes(window);64 bitmap.Save(@"C:\Users\Public\Pictures\Sample Pictures\Window.png", ImageFormat.Png);65 bitmap.Dispose();66 Console.WriteLine("Window image saved to disk");67 Console.ReadLine();68 }69 }70}

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using FlaUI.Core.Capturing;4using System.Windows.Forms;5{6 {7 static void Main(string[] args)8 {9 var rect = Capture.Rectangle();10 MessageBox.Show("X: " + rect.X + " Y: " + rect.Y + " Width: " + rect.Width + " Height: " + rect.Height);11 }12 }13}14using System;15using System.Drawing;16using FlaUI.Core.Capturing;17using System.Windows.Forms;18{19 {20 static void Main(string[] args)21 {22 var rect = Capture.Rectangle();23 MessageBox.Show("X: " + rect.X + " Y: " + rect.Y + " Width: " + rect.Width + " Height: " + rect.Height);24 }25 }26}27using System;28using System.Drawing;29using FlaUI.Core.Capturing;30using System.Windows.Forms;31{32 {33 static void Main(string[] args)34 {35 var rect = Capture.Rectangle();36 MessageBox.Show("X: " + rect.X + " Y: " + rect.Y + " Width: " + rect.Width + " Height: " + rect.Height);37 }38 }39}40using System;41using System.Drawing;42using FlaUI.Core.Capturing;43using System.Windows.Forms;44{45 {46 static void Main(string[] args)47 {48 var rect = Capture.Rectangle();49 MessageBox.Show("X: " + rect.X + " Y: " + rect.Y + " Width: " + rect.Width + " Height: " + rect.Height);50 }51 }52}53using System;54using System.Drawing;55using FlaUI.Core.Capturing;56using System.Windows.Forms;57{58 {59 static void Main(string[] args)60 {

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System.Drawing;3using System;4using System.Windows.Forms;5{6 {7 static void Main(string[] args)8 {9 var rect = new Rectangle(100, 100, 200, 200);10 var image = Capture.Rectangle(rect);11 image.Save("C:\\Users\\Public\\Pictures\\Sample Pictures\\rectangle.bmp");12 }13 }14}

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using FlaUI.Core.Capturing;4{5 {6 static void Main(string[] args)7 {8 Rectangle rectangle = new Rectangle(0, 0, 1920, 1080);9 Capture.Rectangle(rectangle).Save("C:\\Users\\Public\\Documents\\FlaUI\\2.png");10 }11 }12}13using System;14using System.Drawing;15using FlaUI.Core.Capturing;16{17 {18 static void Main(string[] args)19 {20 Rectangle rectangle = new Rectangle(0, 0, 1920, 1080);21 Capture.Rectangle(rectangle).Save("C:\\Users\\Public\\Documents\\FlaUI\\3.png");22 }23 }24}25using System;26using System.Drawing;27using FlaUI.Core.Capturing;28{29 {30 static void Main(string[] args)31 {32 Rectangle rectangle = new Rectangle(0, 0, 1920, 1080);33 Capture.Rectangle(rectangle).Save("C:\\Users\\Public\\Documents\\FlaUI\\4.png");34 }35 }36}37using System;38using System.Drawing;

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System;3using System.Drawing;4using System.Drawing.Imaging;5using System.IO;6{7 {8 public static Bitmap Rectangle(Rectangle rect)9 {10 var bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);11 using (var graphics = Graphics.FromImage(bmp))12 {13 graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);14 }15 return bmp;16 }17 }18}19using FlaUI.Core.Capturing;20using System;21using System.Drawing;22using System.Drawing.Imaging;23using System.IO;24{25 {26 public static Bitmap Rectangle(Rectangle rect)27 {28 var bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);29 using (var graphics = Graphics.FromImage(bmp))30 {31 graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);32 }33 return bmp;34 }35 }36}37using FlaUI.Core.Capturing;38using System;39using System.Drawing;40using System.Drawing.Imaging;41using System.IO;42{43 {44 public static Bitmap Rectangle(Rectangle rect)45 {46 var bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);47 using (var graphics = Graphics.FromImage(bmp))48 {49 graphics.CopyFromScreen(rect.X, rect.Y, 0, 0, rect.Size, CopyPixelOperation.SourceCopy);50 }51 return bmp;52 }53 }54}55using FlaUI.Core.Capturing;56using System;57using System.Drawing;

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System.Drawing;3{4 {5 public static void CaptureRectangle()6 {7 var rect = Capture.Rectangle();8 var bitmap = Capture.CaptureRectangle(rect);9 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample Pictures\\Rectangle.jpg");10 }11 }12}13using FlaUI.Core.Capturing;14using System.Drawing;15{16 {17 public static void CaptureFullScreen()18 {19 var bitmap = Capture.FullScreen();20 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample Pictures\\FullScreen.jpg");21 }22 }23}24using FlaUI.Core.Capturing;25using System.Drawing;26{27 {28 public static void CaptureDesktop()29 {30 var bitmap = Capture.Desktop();31 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample Pictures\\Desktop.jpg");32 }33 }34}35using FlaUI.Core.Capturing;36using System.Drawing;37{38 {39 public static void CaptureWindow()40 {41 var bitmap = Capture.Window();42 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample Pictures\\Window.jpg");43 }44 }45}46using FlaUI.Core.Capturing;47using System.Drawing;48{49 {50 public static void CaptureWindow()51 {52 var bitmap = Capture.Window();53 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample Pictures\\Window.jpg");54 }55 }56}57using FlaUI.Core.Capturing;58using System.Drawing;59{60 {61 public static void CaptureActiveWindow()62 {63 var bitmap = Capture.ActiveWindow();64 bitmap.Save("C

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1public static void Main()2{3 var window = FlaUI.Core.Application.GetApplication("notepad").GetMainWindow(FlaUI.Core.Automation);4 var rectangle = FlaUI.Core.Capturing.Capture.Rectangle(window);5 var bitmap = new Bitmap(rectangle.Width, rectangle.Height);6 using (var graphics = Graphics.FromImage(bitmap))7 {8 graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);9 }10 bitmap.Save("test.png");11}12public static void Main()13{14 var window = FlaUI.Core.Application.GetApplication("notepad").GetMainWindow(FlaUI.Core.Automation);15 var rectangle = FlaUI.Core.Capturing.Capture.Rectangle(window);16 var bitmap = new Bitmap(rectangle.Width, rectangle.Height);17 using (var graphics = Graphics.FromImage(bitmap))18 {19 graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);20 }21 bitmap.Save("test.png");22}23public static void Main()24{25 var window = FlaUI.Core.Application.GetApplication("notepad").GetMainWindow(FlaUI.Core.Automation);26 var rectangle = FlaUI.Core.Capturing.Capture.Rectangle(window);27 var bitmap = new Bitmap(rectangle.Width, rectangle.Height);28 using (var graphics = Graphics.FromImage(bitmap))29 {30 graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);31 }32 bitmap.Save("test.png");33}34public static void Main()35{36 var window = FlaUI.Core.Application.GetApplication("notepad").GetMainWindow(FlaUI.Core.Automation);37 var rectangle = FlaUI.Core.Capturing.Capture.Rectangle(window);38 var bitmap = new Bitmap(rectangle.Width, rectangle.Height);39 using (var graphics = Graphics.FromImage(bitmap))40 {41 graphics.CopyFromScreen(rectangle.X, rectangle.Y, 0, 0, rectangle.Size, CopyPixelOperation.SourceCopy);42 }43 bitmap.Save("test.png");44}

Full Screen

Full Screen

Rectangle

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Windows.Forms;4using FlaUI.Core.Capturing;5using System.Drawing.Imaging;6{7 {8 public Form1()9 {10 InitializeComponent();11 }12 private void button1_Click(object sender, EventArgs e)13 {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run FlaUI automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful