19using System.Drawing.Imaging;
22using System.Windows.Forms;
39 Rectangle
FindImage(Bitmap screen,
string templatePath,
int matchingPercentage);
58 private static string base64StringInternal =
null;
94 base64StringInternal =
null;
96 bool headlessMode =
false;
97 Bitmap image =
new Bitmap(1920, 1080);
98 bool isBrowser = typeof(
Browser.Browser) == ApplicationOrBrowser?.GetType();
99 if (
true == isBrowser)
105 || ((
Browser.Browser)ApplicationOrBrowser).BrowserSettings.HiddenDesktop
110 image =
new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
116 image =
new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
122 System.Diagnostics.Debug.WriteLine(
"new Bitmap failed: Unable to take screenshot");
127 if (
false == headlessMode)
129 var gfx = Graphics.FromImage(image);
132 gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
136 else if (
true == isBrowser)
142 byte[] imageBytes =
null;
144 if (
true == ((
Browser.Browser)ApplicationOrBrowser).UseSelenium)
146 if (
null != ((
Browser.Browser)ApplicationOrBrowser).BrowserDriver)
148 Screenshot screenshot = ((ITakesScreenshot)((
Browser.Browser)ApplicationOrBrowser).BrowserDriver).GetScreenshot();
150 base64StringInternal = screenshot.AsBase64EncodedString;
153 else if (0 <
GPAL.Browsers.Count())
156 if (
null != browser.BrowserSettings.Process)
158 if (
true == ((
Browser.Browser)ApplicationOrBrowser).UsePuppeteer)
162 else if (((
Browser.Browser)ApplicationOrBrowser).UseOttoMagic)
164 base64StringInternal = ((
Browser.Browser)ApplicationOrBrowser).
MagicHelper.CaptureVisibleTab();
176 base64StringInternal = base64StringInternal.Trim()
183 base64StringInternal = base64StringInternal.Trim()
188 base64StringInternal = base64StringInternal.Trim();
190 imageBytes = Convert.FromBase64String(base64StringInternal);
191 using (MemoryStream ms =
new MemoryStream(imageBytes))
193 image =
new Bitmap(ms);
197 string outMessageHeader =
$@"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff
")}][EXCEPTION][ImageHelper]: ";
201 if (
"1" != Environment.GetEnvironmentVariable(
"GPAL_IS_SILENT"))
203 Console.WriteLine($
"{outMessageHeader} [{Browser.AutomationEngine}][{Browser.BrowserType}][{(true == Browser.BrowserSettings.UseHeadless ? "Headless
" : "Headful
")}] Unable to take screenshot [{ex.Message}]");
204 System.Diagnostics.Debug.WriteLine($
"{outMessageHeader} [{Browser.AutomationEngine}][{Browser.BrowserType}][{(true == Browser.BrowserSettings.UseHeadless ? "Headless
" : "Headful
")}] Unable to take screenshot [{ex.Message}]");
228 System.IO.MemoryStream ms =
new MemoryStream();
229 if (
null != base64StringInternal)
230 base64String = base64StringInternal;
234 byte[] byteImage = ms.ToArray();
235 base64String = Convert.ToBase64String(byteImage);
238 base64String =
string.Empty;
265 [File(Description =
"Save to file (overwrite)")]
278 private static void SaveScreenshot(
string fileName, Image screenshot)
282 if (
null != screenshot)
284 string saveToDirectory = Path.GetDirectoryName(fileName);
287 if (
false ==
string.IsNullOrEmpty(saveToDirectory))
288 Directory.CreateDirectory(saveToDirectory);
292 screenshot.Save(fileName, FormatForFile(fileName));
297 GPAL.PublishSimpleEvent(Enums.GPALEventType.WARNING,
"No screenshot to save");
301 GPAL.PublishSimpleEvent(Enums.GPALEventType.EXCEPTION, $
"No screenshot saved to [{fileName}]",
null, Enums.GPALObjectType.None, ex);
310 internal static System.Drawing.Imaging.ImageFormat FormatForFile(
string filename)
312 System.Drawing.Imaging.ImageFormat retVal;
314 switch (Path.GetExtension(filename ??
string.Empty).ToLower())
318 retVal = System.Drawing.Imaging.ImageFormat.Jpeg;
321 retVal = System.Drawing.Imaging.ImageFormat.Bmp;
324 retVal = System.Drawing.Imaging.ImageFormat.Gif;
328 retVal = System.Drawing.Imaging.ImageFormat.Tiff;
331 retVal = System.Drawing.Imaging.ImageFormat.Png;
352 return System.Drawing.Image.FromFile(filename);
367 base64String = base64String.Trim().Trim(
'\"');
372 string pattern =
@"^data:[^;]+;base64,";
373 base64String = System.Text.RegularExpressions.Regex.Replace(base64String, pattern,
string.Empty);
376 byte[] imageBytes = Convert.FromBase64String(base64String);
380 MemoryStream ms =
new MemoryStream(imageBytes, 0, imageBytes.Length);
382 Image image = Image.FromStream(ms,
true);
402 public Rectangle
FindImage(
string imagePath,
object ApplicationOrBrowser =
null)
404 Rectangle retVal = Rectangle.Empty;
406 if (
null ==
GPAL.ImageMatcher)
409 if (
false == matcherMissingSaid)
411 matcherMissingSaid =
true;
412 GPAL.
PublishSimpleEvent(Enums.GPALEventType.WARNING, $
"Image matching needs the GPAL.Image package, added and registered with GPALImage.Register() at startup, so [{imagePath}] was not looked for");
428 GPAL.
PublishSimpleEvent(Enums.GPALEventType.EXCEPTION, $
"Error locating [{imagePath}] on-screen.",
null, Enums.GPALObjectType.None, ex);
436 private static bool matcherMissingSaid;
453 internal static class ImageExtensions
461 public static byte[] ToByteArray(
this System.Drawing.Image image, ImageFormat format)
463 using (MemoryStream ms =
new MemoryStream())
465 image.Save(ms, format);
Browser object that contains the fluent methods to create your Browser workflow. Instatiated using G...
IAllowPuppeteerExecution CaptureVisibleTab(ImageFormat imageFormat=ImageFormat.JPEG)
Configure this client to capture a screenshot of the visible tab in the given image format.
GPAL File object instantied with GPAL.File Used to load tokens into a GPALGrid [rows/columns].
GPALFile Next
Advances an internal cursor and returns the next filename to use. If there is exactly one file and Wi...
Everything starts here, all the GPAL controls and global settings using fluent syntax are here....
static void PublishSimpleEvent(GPALEventType gPALEventType, string msg, dynamic gPALObject=null, Enums.GPALObjectType gPALObjectType=GPALObjectType.None, Exception ex=null)
Publish a message to either the information channel or exception channel (if exception passed in) Pub...
Provides comprehensive image capture, conversion, and matching capabilities for automation scenarios....
static Bitmap ScreenShot
Static reference to the most recent screenshot captured by this helper.
IImageHelper ToGPALObject()
Returns this instance as IImageHelper for interface-based usage.
IAllowGraphicSettings SaveTo(GPALFile file)
Saves the captured screenshot to a file (overwrites if exists).
IAllowGraphicSettings CaptureScreen(object ApplicationOrBrowser=null)
Captures the current screen or visible browser tab. Works in both headful (desktop screenshot) and he...
IAllowGraphicSettings ToBitmap(out Bitmap bitmap)
Returns the last captured screenshot as a Bitmap.
IAllowGraphicSettings ToBase64String(out string base64String)
Converts the last captured screenshot to a Base64 string. A browser capture comes back in whatever th...
static Image Base64ToImage(string base64String)
Converts a Base64 string (with or without data URI prefix) to a System.Drawing.Image.
Rectangle FindImage(string imagePath, object ApplicationOrBrowser=null)
Finds the location of a template image within the current screen/browser screenshot using OpenCV temp...
Browser.Browser Browser
Gets or sets the associated Browser instance for headless capture operations.
static System.Drawing.Image LoadImageFromFile(string filename)
Loads an image from the specified file path.
Finds a picture on a capture of the screen or of a browser. The GPAL.Image package provides one and r...
Rectangle FindImage(Bitmap screen, string templatePath, int matchingPercentage)
Where the picture in templatePath is on screen .