78 bool headlessMode =
false;
79 Bitmap image =
new Bitmap(1920, 1080);
80 bool isBrowser = typeof(
Browser.Browser) == ApplicationOrBrowser?.GetType();
81 if (
true == isBrowser)
86 || ((
Browser.Browser)ApplicationOrBrowser).BrowserSettings.HiddenDesktop;
90 image =
new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
96 image =
new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb);
102 System.Diagnostics.Debug.WriteLine(
"new Bitmap failed: Unable to take screenshot");
107 if (
false == headlessMode)
109 var gfx = Graphics.FromImage(image);
112 gfx.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy);
116 else if (
true == isBrowser)
122 byte[] imageBytes =
null;
124 if (
true == ((
Browser.Browser)ApplicationOrBrowser).UseSelenium)
126 if (
null != ((
Browser.Browser)ApplicationOrBrowser).BrowserDriver)
128 Screenshot screenshot = ((ITakesScreenshot)((
Browser.Browser)ApplicationOrBrowser).BrowserDriver).GetScreenshot();
130 base64StringInternal = screenshot.AsBase64EncodedString;
133 else if (0 <
GPAL.Browsers.Count())
136 if (
null != browser.BrowserSettings.Process)
138 if (
true == ((
Browser.Browser)ApplicationOrBrowser).UsePuppeteer)
142 else if (((
Browser.Browser)ApplicationOrBrowser).UseOttoMagic)
144 base64StringInternal = ((
Browser.Browser)ApplicationOrBrowser).
MagicHelper.CaptureVisibleTab();
156 base64StringInternal = base64StringInternal.Trim()
163 base64StringInternal = base64StringInternal.Trim()
168 base64StringInternal = base64StringInternal.Trim();
170 imageBytes = Convert.FromBase64String(base64StringInternal);
171 using (MemoryStream ms =
new MemoryStream(imageBytes))
173 image =
new Bitmap(ms);
177 string outMessageHeader =
$@"[{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffff
")}][EXCEPTION][ImageHelper]: ";
181 if (
"1" != Environment.GetEnvironmentVariable(
"GPAL_IS_SILENT"))
183 Console.WriteLine($
"{outMessageHeader} [{Browser.AutomationEngine}][{Browser.BrowserType}][{(true == Browser.BrowserSettings.UseHeadless ? "Headless
" : "Headful
")}] Unable to take screenshot [{ex.Message}]");
184 System.Diagnostics.Debug.WriteLine($
"{outMessageHeader} [{Browser.AutomationEngine}][{Browser.BrowserType}][{(true == Browser.BrowserSettings.UseHeadless ? "Headless
" : "Headful
")}] Unable to take screenshot [{ex.Message}]");
283 GPAL.PublishSimpleEvent(Enums.GPALEventType.EXCEPTION, $
"No screenshot saved to [{fileName}]",
null, Enums.GPALObjectType.None, ex);
316 base64String = base64String.Trim().Trim(
'\"');
318 string pattern =
@"data:[^;]+;base64,[A-Za-z0-9+/=]+";
319 base64String = System.Text.RegularExpressions.Regex.Replace(base64String, pattern,
string.Empty);
322 byte[] imageBytes = Convert.FromBase64String(base64String);
323 MemoryStream ms =
new MemoryStream(imageBytes, 0, imageBytes.Length);
326 ms.Write(imageBytes, 0, imageBytes.Length);
327 Image image = Image.FromStream(ms,
true);
347 public Rectangle
FindImage(
string imagePath,
object ApplicationOrBrowser =
null)
349 Rectangle screenRect = Screen.PrimaryScreen.Bounds;
350 Rect match =
new Rect();
361 Mat sourceMat = OpenCvSharp.Extensions.BitmapConverter.ToMat(screenshot);
362 Mat templateMat =
new Mat(imagePath);
363 Mat sourceGray =
new Mat();
364 Mat templateGray =
new Mat();
367 if (sourceMat.Empty())
369 GPAL.
PublishSimpleEvent(Enums.GPALEventType.WARNING,
"SourceMat is empty.", sourceMat, Enums.GPALObjectType.Other);
370 return new Rectangle();
372 if (templateMat.Empty())
374 GPAL.
PublishSimpleEvent(Enums.GPALEventType.WARNING,
"TemplateMat is empty.", templateMat, Enums.GPALObjectType.Other);
375 return new Rectangle();
379 Cv2.CvtColor(sourceMat, sourceGray, ColorConversionCodes.BGR2GRAY);
380 Cv2.CvtColor(templateMat, templateGray, ColorConversionCodes.BGR2GRAY);
383 Mat result =
new Mat();
384 Cv2.MatchTemplate(sourceGray, templateGray, result, TemplateMatchModes.CCoeffNormed);
387 Cv2.MinMaxLoc(result, out _, out
double res, out _, out OpenCvSharp.Point maxLoc);
390 if (res >= (
GPAL.MatchingPercentage * 0.01))
391 match =
new Rect(maxLoc.X, maxLoc.Y, templateMat.Width, templateMat.Height);
395 GPAL.
PublishSimpleEvent(Enums.GPALEventType.EXCEPTION, $
"Error locating [{imagePath}] on-screen.",
null, Enums.GPALObjectType.None, ex);
398 return new Rectangle(match.X, match.Y, match.Width, match.Height);