How to use DeleteObject method of FlaUI.Core.Capturing.CaptureUtilities class

Best FlaUI code snippet using FlaUI.Core.Capturing.CaptureUtilities.DeleteObject

CaptureUtilities.cs

Source:CaptureUtilities.cs Github

copy

Full Screen

...11 /// </summary>12 public static class CaptureUtilities13 {14 [DllImport("Gdi32")]15 public static extern bool DeleteObject(IntPtr ho);16 /// <summary>17 /// Calculates a scale factor according to the bounds and capture settings.18 /// </summary>19 /// <param name="originalBounds">The original bounds of the captured image.</param>20 /// <param name="captureSettings">The settings to use for the capture.</param>21 /// <returns>A scale factor, defaults to 1 which means original size.</returns>22 public static double GetScale(Rectangle originalBounds, CaptureSettings captureSettings)23 {24 double scale = 1;25 if (captureSettings != null)26 {27 scale = captureSettings.OutputScale;28 if (scale == 1)29 {30 if (captureSettings.OutputHeight == -1 && captureSettings.OutputWidth != -1)31 {32 // Calculate the scale by a defined width33 scale = captureSettings.OutputWidth / (double)originalBounds.Width;34 }35 else if (captureSettings.OutputHeight != -1 && captureSettings.OutputWidth == -1)36 {37 // Calculate the scale by a defined height38 scale = captureSettings.OutputHeight / (double)originalBounds.Height;39 }40 }41 }42 return scale;43 }44 /// <summary>45 /// Scales a point according to the given settings.46 /// </summary>47 /// <param name="x">The x-position of the point to scale.</param>48 /// <param name="y">The y-position of the pint to scale.</param>49 /// <param name="originalBounds">The original bounds of the captured image.</param>50 /// <param name="captureSettings">The settings to use for the capture.</param>51 /// <returns>The transformed point.</returns>52 public static Point ScaleAccordingToSettings(int x, int y, Rectangle originalBounds, CaptureSettings captureSettings)53 {54 return ScaleAccordingToSettings(new Point(x, y), originalBounds, captureSettings);55 }56 /// <summary>57 /// Scales a point according to the given settings.58 /// </summary>59 /// <param name="point">The point to scale.</param>60 /// <param name="originalBounds">The original bounds of the captured image.</param>61 /// <param name="captureSettings">The settings to use for the capture.</param>62 /// <returns>The transformed point.</returns>63 public static Point ScaleAccordingToSettings(Point point, Rectangle originalBounds, CaptureSettings captureSettings)64 {65 var scale = GetScale(originalBounds, captureSettings);66 return scale != 1 ? new Point((point.X * scale).ToInt(), (point.Y * scale).ToInt()) : point;67 }68 /// <summary>69 /// Scales a rectangle according to the given settings.70 /// </summary>71 /// <param name="originalBounds">The original bounds of the captured image.</param>72 /// <param name="captureSettings">The settings to use for the capture.</param>73 /// <returns>The transformed rectangle.</returns>74 public static Rectangle ScaleAccordingToSettings(Rectangle originalBounds, CaptureSettings captureSettings)75 {76 // Default is the original size77 var outputWidth = originalBounds.Width;78 var outputHeight = originalBounds.Height;79 if (captureSettings != null)80 {81 if (captureSettings.OutputScale != 1)82 {83 // Calculate with the scale84 outputWidth = (originalBounds.Width * captureSettings.OutputScale).ToInt();85 outputHeight = (originalBounds.Height * captureSettings.OutputScale).ToInt();86 }87 else if (captureSettings.OutputHeight == -1 && captureSettings.OutputWidth != -1)88 {89 // Adjust the height90 outputWidth = captureSettings.OutputWidth;91 var percent = outputWidth / (double)originalBounds.Width;92 outputHeight = (originalBounds.Height * percent).ToInt();93 }94 else if (captureSettings.OutputHeight != -1 && captureSettings.OutputWidth == -1)95 {96 // Adjust the width97 outputHeight = captureSettings.OutputHeight;98 var percent = outputHeight / (double)originalBounds.Height;99 outputWidth = (originalBounds.Width * percent).ToInt();100 }101 }102 return new Rectangle(0, 0, outputWidth, outputHeight);103 }104 /// <summary>105 /// Captures the cursor as bitmap and returns the bitmap and the position on screen of the cursor.106 /// </summary>107 public static Bitmap CaptureCursor(ref Point position)108 {109 var cursorInfo = new CURSORINFO();110 cursorInfo.cbSize = Marshal.SizeOf(cursorInfo);111 if (!User32.GetCursorInfo(out cursorInfo))112 {113 return null;114 }115 if (cursorInfo.flags != CursorState.CURSOR_SHOWING)116 {117 return null;118 }119 var hicon = User32.CopyIcon(cursorInfo.hCursor);120 if (hicon == IntPtr.Zero)121 {122 return null;123 }124 if (!User32.GetIconInfo(hicon, out var iconInfo))125 {126 return null;127 }128 // Calculate the position respecting the hotspot offset129 position.X = cursorInfo.ptScreenPos.X - iconInfo.xHotspot;130 position.Y = cursorInfo.ptScreenPos.Y - iconInfo.yHotspot;131 using (var maskBitmap = Image.FromHbitmap(iconInfo.hbmMask))132 {133 // Special handling for monchome icons134 if (maskBitmap.Height == maskBitmap.Width * 2)135 {136 var cursor = new Bitmap(maskBitmap.Width, maskBitmap.Width, PixelFormat.Format32bppArgb);137 var black = Color.FromArgb(255, 0, 0, 0); //cannot compare Color.Black because of different names138 var white = Color.FromArgb(255, 255, 255, 255); //cannot compare Color.White because of different names139 for (var y = 0; y < maskBitmap.Width; y++)140 {141 for (var x = 0; x < maskBitmap.Width; x++)142 {143 var maskPixel = maskBitmap.GetPixel(x, y);144 var cursorPixel = maskBitmap.GetPixel(x, y + maskBitmap.Width);145 if (maskPixel == white && cursorPixel == black)146 {147 cursor.SetPixel(x, y, Color.Transparent);148 }149 else if (maskPixel == black)150 {151 cursor.SetPixel(x, y, cursorPixel);152 }153 else154 {155 cursor.SetPixel(x, y, cursorPixel == black ? white : black);156 }157 }158 }159 return cursor;160 }161 }162 // Just return the icon converted to a bitmap163 Bitmap result;164 using (Icon icon = Icon.FromHandle(hicon))165 {166 result = icon.ToBitmap();167 }168 User32.DestroyIcon(hicon);169 DeleteObject(iconInfo.hbmMask);170 DeleteObject(iconInfo.hbmColor);171 return result;172 }173 }174}...

Full Screen

Full Screen

Capture.cs

Source:Capture.cs Github

copy

Full Screen

...99 var hPrevBmp = Gdi32.SelectObject(hDest, hBmp);100 action(hDest, hSrc);101 var bmp = Image.FromHbitmap(hBmp);102 Gdi32.SelectObject(hDest, hPrevBmp);103 Gdi32.DeleteObject(hBmp);104 Gdi32.DeleteDC(hDest);105 User32.ReleaseDC(hDesk, hSrc);106 return bmp;107 }108 }109}

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System;3using System.Drawing;4using System.Windows.Forms;5{6 {7 public Form1()8 {9 InitializeComponent();10 }11 private void button1_Click(object sender, EventArgs e)12 {13 Bitmap bmp = new Bitmap(100, 100);14 Graphics g = Graphics.FromImage(bmp);15 Pen p = new Pen(Color.Red);16 g.DrawArc(p, 0, 0, 100, 100, 0, 90);17 PictureBox pb = new PictureBox();18 pb.Image = bmp;19 this.Controls.Add(pb);20 CaptureUtilities.DeleteObject(bmp);21 g.Dispose();22 }23 }24}

Full Screen

Full Screen

DeleteObject

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.Runtime.InteropServices;9{10 {11 public static Bitmap CaptureScreen()12 {13 return CaptureScreen(0, 0, 0, 0);14 }15 public static Bitmap CaptureScreen(int x, int y, int width, int height)16 {17 var hdcSrc = GetWindowDC(IntPtr.Zero);18 var hdcDest = CreateCompatibleDC(hdcSrc);19 var hBitmap = CreateCompatibleBitmap(hdcSrc, width, height);20 var hOld = SelectObject(hdcDest, hBitmap);21 BitBlt(hdcDest, 0, 0, width, height, hdcSrc, x, y, 13369376);22 SelectObject(hdcDest, hOld);23 DeleteObject(hBitmap);24 DeleteDC(hdcDest);25 DeleteDC(hdcSrc);26 return Image.FromHbitmap(hBitmap);27 }28 [DllImport("gdi32.dll")]29 private static extern int DeleteDC(IntPtr hdc);30 [DllImport("gdi32.dll")]31 private static extern IntPtr DeleteObject(IntPtr hObject);32 [DllImport("gdi32.dll")]33 private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);34 [DllImport("gdi32.dll")]35 private static extern IntPtr CreateCompatibleDC(IntPtr hdc);36 [DllImport("gdi32.dll")]37 private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);38 [DllImport("gdi32.dll")]39 private static extern int BitBlt(IntPtr hdc, int x, int y, int cx, int cy, IntPtr hdcSrc, int x1, int y1, int rop);40 [DllImport("user32.dll")]41 private static extern IntPtr GetWindowDC(IntPtr hWnd);42 }43}44using System;45using System.Collections.Generic;46using System.Linq;47using System.Text;48using System.Threading.Tasks;49using System.Windows.Forms;50using System.Drawing;51using System.Runtime.InteropServices;52{53 {54 public static Bitmap CaptureScreen()55 {56 return CaptureScreen(0, 0, 0, 0);57 }

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System;3using System.Drawing;4using System.Windows.Forms;5{6 {7 static void Main(string[] args)8 {9 Bitmap bmp = new Bitmap(200, 200);10 Graphics g = Graphics.FromImage(bmp);11 g.DrawRectangle(new Pen(Color.Black), new Rectangle(0, 0, 200, 200));12 bmp.Save("C:\\Users\\Public\\Pictures\\Sample.bmp");13 CaptureUtilities.DeleteObject(bmp.GetHbitmap());14 }15 }16}

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.Drawing.Imaging;4using System.IO;5using FlaUI.Core.Capturing;6{7 {8 static void Main(string[] args)9 {10 Bitmap myBitmap = new Bitmap("c:\\temp\\test.png");11 IntPtr hBitmap = myBitmap.GetHbitmap();12 CaptureUtilities.DeleteObject(hBitmap);13 }14 }15}

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System;3using System.Drawing;4using FlaUI.Core;5using FlaUI.Core.AutomationElements;6using FlaUI.Core.Input;7using FlaUI.Core.WindowsAPI;8using FlaUI.UIA3;9{10 {11 static void Main(string[] args)12 {13 var application = Application.Launch("notepad.exe");14 var automation = new UIA3Automation();15 var window = application.GetMainWindow(automation);16 var button = window.FindFirstDescendant(cf => cf.ByAutomationId("15"));17 button.Click();18 var image = CaptureUtilities.CaptureWindow(window);19 image.Save("notepad.png");20 var buttonImage = CaptureUtilities.CaptureElement(button);21 buttonImage.Save("button.png");22 var buttonImageMargin = CaptureUtilities.CaptureElement(button, 10);23 buttonImageMargin.Save("buttonMargin.png");24 var buttonImageMarginBorder = CaptureUtilities.CaptureElement(button, 10, 2);25 buttonImageMarginBorder.Save("buttonMarginBorder.png");26 var buttonImageMarginBorderShadow = CaptureUtilities.CaptureElement(button, 10, 2, 4);27 buttonImageMarginBorderShadow.Save("buttonMarginBorderShadow.png");28 var buttonImageMarginBorderShadowHighlight = CaptureUtilities.CaptureElement(button, 10, 2, 4, 5);

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using FlaUI.Core.Definitions;3using FlaUI.Core.Input;4using FlaUI.Core.WindowsAPI;5using System;6using System.Drawing;7using System.Threading;8{9 {10 static void Main(string[] args)11 {12 var automation = FlaUI.Core.Automation.AutomationFactory.GetAutomation();13 var desktopWindow = automation.GetDesktopWindow();14 var notepadWindow = desktopWindow.FindFirstChild(cf => cf.ByName("Untitled - Notepad"));15 notepadWindow.Focus();16 Mouse.Instance.Location = notepadWindow.GetClickablePoint();17 Mouse.Instance.Click(MouseButton.Left);18 Keyboard.Instance.Enter("Hello World!");19 Thread.Sleep(5000);20 var bitmap = CaptureUtilities.CaptureWindow(notepadWindow);21 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample.bmp", System.Drawing.Imaging.ImageFormat.Bmp);22 var imageHandle = bitmap.GetHbitmap();23 var graphics = Graphics.FromHbitmap(imageHandle);24 graphics.DrawRectangle(new Pen(Color.Red, 3), new Rectangle(0, 0, 200, 200));25 bitmap.Save("C:\\Users\\Public\\Pictures\\Sample2.bmp", System.Drawing.Imaging.ImageFormat.Bmp);26 NativeMethods.DeleteObject(imageHandle);27 }28 }29}

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using System;2using FlaUI.Core.Capturing;3{4 {5 static void Main(string[] args)6 {7 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(@"C:\Users\Public\Pictures\Sample Pictures\Koala.jpg");8 CaptureUtilities.DeleteObject(bmp.GetHbitmap());9 Console.WriteLine("Object deleted successfully");10 }11 }12}

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using FlaUI.Core.Capturing;2using System;3using System.Drawing;4{5 {6 static void Main(string[] args)7 {8 Bitmap bmp = new Bitmap(100, 100);9 Graphics g = Graphics.FromImage(bmp);10 g.DrawRectangle(Pens.Red, 0, 0, 100, 100);11 bmp.Save("rectangle.bmp");12 g.Dispose();13 bmp.Dispose();14 CaptureUtilities.DeleteObject("rectangle.bmp");15 }16 }17}

Full Screen

Full Screen

DeleteObject

Using AI Code Generation

copy

Full Screen

1using System;2using System.Drawing;3using System.IO;4using FlaUI.Core.Capturing;5{6 {7 static void Main(string[] args)8 {9 Bitmap bitmap = CaptureUtilities.CaptureRegion(new System.Windows.Rect(0, 0, 100, 100));10 bitmap.Save(@"C:\Users\Public\Pictures\SampleScreenshot.bmp");11 }12 }13}14using System;15using System.Drawing;16using System.IO;17using FlaUI.Core.Capturing;18{19 {20 static void Main(string[] args)21 {22 Bitmap bitmap = CaptureUtilities.CaptureScreen();23 bitmap.Save(@"C:\Users\Public\Pictures\SampleScreenshot.bmp");24 }25 }26}27using System;28using System.Drawing;29using System.IO;30using FlaUI.Core.Capturing;31{32 {33 static void Main(string[] args)34 {35 Bitmap bitmap = CaptureUtilities.CaptureActiveWindow();36 bitmap.Save(@"C:\Users\Public\Pictures\SampleScreenshot.bmp");37 }38 }39}40using System;41using System.Drawing;42using System.IO;43using FlaUI.Core.Capturing;44{45 {46 static void Main(string[] args)47 {48 Bitmap bitmap = CaptureUtilities.CaptureApplication();49 bitmap.Save(@"C:\Users\Public\Pictures\SampleScreenshot.bmp");50 }51 }52}53using System;54using System.Drawing;55using System.IO;

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.

Most used method in CaptureUtilities

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful