using LoginPI.Engine.ScriptBase; using System; using System.Runtime.InteropServices; using System.Drawing; using System.Drawing.Imaging; public class YoutubeStatsScreenshot : ScriptBase { private void Execute() { /* Metadata: -Utilizing Windows 10 Professional x86; winver 1909; OS Build 18363.476 -ScriptingToolSet ver. 3.6.23 -Scripting date: V1.0: Dec 22 2019 -Crafted using: -Google Chrome Version 79.0.3945.88 (64-bit) -youtube.com */ // Please set the following variables ///////////////////// Starting of variables to define ///////////////////// // Set the path to save the file; example: c:\temp\ or \\myserver\myshare\picturepath\ string screenshotOutPath = @"c:\temp\"; int GlobalTimeoutInSeconds = 60; // Define how long it should take maximum for operations to complete, such as finding the Chrome browser, in seconds. This script will fail, as designed, if this value is surpassed; example: 60 int WaitSeconds = 1; // Define here how many seconds to wait in between functions; example: 2. This will allow for the target application's UI to catch up ///////////////////// Ending of variables to define ///////////////////// string hostName = System.Net.Dns.GetHostName(); var userName = Environment.GetEnvironmentVariable("username"); var timeStamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(); var YouTubeWindow = FindWindow(title:"*YouTube -*",timeout:GlobalTimeoutInSeconds); Wait(WaitSeconds); YouTubeWindow.Maximize(); YouTubeWindow.Focus(); Wait(WaitSeconds); var imageSave = ScreenCapture.CaptureActiveWindow(); imageSave.Save(screenshotOutPath + userName + "_" + hostName + "_unixTime" + timeStamp + ".png", ImageFormat.Png); } } public class ScreenCapture { [DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] public static extern IntPtr GetDesktopWindow(); [StructLayout(LayoutKind.Sequential)] private struct Rect { public int Left; public int Top; public int Right; public int Bottom; } [DllImport("user32.dll")] private static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect); public static Bitmap CaptureActiveWindow() { return CaptureWindow(GetForegroundWindow()); } public static Bitmap CaptureWindow(IntPtr handle) { var rect = new Rect(); GetWindowRect(handle, ref rect); var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top); var result = new Bitmap(bounds.Width, bounds.Height); using (var graphics = Graphics.FromImage(result)) { graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size); } return result; } }