using System; using System.Drawing; using System.Windows.Forms; using System.Drawing.Imaging; using CefSharp; using CefSharp.WinForms; using CommandLine; using System.Collections.Generic; using System.IO; namespace ScreenshotForm { public partial class ScreenShotForm : Form { public ChromiumWebBrowser browser; private int variableNum = 1; private string path; private bool noClose = false; private Options _options = null; public class Options { [Option('u', "url", Required = false, Default = null, HelpText = "url to use")] public string Url { get; set; } [Option('o', "output", Required = true, HelpText = "Enter the Path and the output image name, please include the .PNG at the end")] public string Path { get; set; } [Option('w', "width", Required = false, Default = 800, HelpText = "width in pixels for browser area")] public int Width { get; set; } [Option('h', "height", Required = false, Default = 600, HelpText = "height in pixels for browser area")] public int Height { get; set; } [Option('c', "no-exit", Default = false, Required = false, HelpText = "no-exit means that the progrma will not exit after taking the initial screenshot")] public bool NoClose { get; set; } //For the Screenshot [Option('x', "screenshot-x", Required = false, Default = 0, HelpText = "width in pixels for browser area")] public int ScreenX { get; set; } [Option('y', "screenshot-y", Required = false, Default = 0, HelpText = "width in pixels for browser area")] public int ScreenY { get; set; } [Option('l', "screenshot-width", Required = false, Default = 0, HelpText = "width for the screenshot")] public int ScreenW { get; set; } [Option('t', "screenshot-height", Required = false, Default = 0, HelpText = "height for the screenshot")] public int ScreenH { get; set; } } private object HandleParseError(IEnumerable errs) { closeForm(); return 1; } private object RunOptionsAndReturnExitCode(Options options) { _options = options; return null; } public ScreenShotForm() { string[] args = Environment.GetCommandLineArgs(); var result = CommandLine.Parser.Default.ParseArguments(args).MapResult( (opts) => RunOptionsAndReturnExitCode(opts), errs => HandleParseError(errs)); path = _options.Path; noClose = _options.NoClose; if(_options.Url != null) _options.Url = _options.Url.Replace("\"", ""); InitializeComponent(); InitBrowser(); BrowserSetup(_options.Url, _options.Width, _options.Height); } public void InitBrowser() { var settings = new CefSettings(); //this may be needed on lower-end computers settings.CefCommandLineArgs.Add("disable-gpu"); Cef.Initialize(settings); } public void BrowserSetup(string url, int width, int height) { label.Text = "Loading " + url + " it may take a bit"; browser = new ChromiumWebBrowser(url); chromiumWebBrowser.Controls.Add(browser); chromiumWebBrowser.Width = width; chromiumWebBrowser.Height = height; this.ClientSize = new System.Drawing.Size(width + 40, height + 70); chromiumWebBrowser.Load(url); } //Submit private void Submit_Click(object sender, EventArgs e) { //Updates on what the is being loaded label.Text = "Loading " + textBox.Text + " it may take a bit"; if (File.Exists(path)) { while (Char.IsDigit(path[path.Length - 5])) path = path.Remove(path.Length - 5, 1); path = path.Insert(path.Length - 4, variableNum.ToString()); } //This makes the 2 browsers load the same thing (1 browser is what we see, and the other is what is screenshotted) browser = new ChromiumWebBrowser(textBox.Text); chromiumWebBrowser.Load(textBox.Text); chromiumWebBrowser.Controls.Clear(); chromiumWebBrowser.Controls.Add(browser); variableNum++; } //If the browser is changed private void WebBrowser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e) { //Once the Page is loaded if (e.IsLoading == false) { //needs to be invoked since they are on different threads label.Invoke(new MethodInvoker(delegate {; label.Text = chromiumWebBrowser.Address + " is loaded and the path is " + path; })); if (_options.ScreenW == 0) _options.ScreenW = _options.Width - _options.ScreenX; if (_options.ScreenH == 0) _options.ScreenH = _options.Height - _options.ScreenY; //Takes the screenshot Bitmap theScreenshot = ControlSnapshot.Snapshot(browser, _options.ScreenX, _options.ScreenY, _options.ScreenW, _options.ScreenH); theScreenshot.Save(@path, ImageFormat.Png); if (noClose == false) closeForm(); } } public void closeForm() { this.Invoke(new MethodInvoker(delegate {; this.Close(); })); } } }