public static async Task <int> TakeScreenshotAsync(string url, int width, int height, string[] args) { try { var mainArgs = new CefMainArgs(args); // create app var app = new OffscreenApp(); var exitCode = CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); if (exitCode != -1) { return(exitCode); } // init CEF var settings = new CefSettings { SingleProcess = false, MultiThreadedMessageLoop = true, LogSeverity = CefLogSeverity.Error, LogFile = "CefGlue.log" }; CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero); // create browser var browser = new OffscreenBrowser(url, width, height); // wait for the site to be loaded await WaitForBrowserLoadingAsync(browser).ConfigureAwait(false); await Task.Delay(1000).ConfigureAwait(false); browser.WindowBitmap.Save("output.png"); // dispose everything var host = browser.GetBrowser().GetHost(); host.CloseBrowser(); host.Dispose(); CefRuntime.Shutdown(); return(0); } catch (Exception exc) { Console.WriteLine(exc.Message); } return(-1); }
public override void Run() { var mainArgs = new CefMainArgs(new string[] { }); var app = new OffscreenApp(); CefRuntime.ExecuteProcess(mainArgs, app, IntPtr.Zero); // init CEF var settings = new CefSettings { SingleProcess = false, MultiThreadedMessageLoop = true, LogSeverity = CefLogSeverity.Error, LogFile = "CefGlue.log" }; CefRuntime.Initialize(mainArgs, settings, app, IntPtr.Zero); Trace.WriteLine("Starting processing of messages"); // Initiates the message pump and callback is invoked for each message that is received, calling close on the client will stop the pump. Client.OnMessage((receivedMessage) => { try { // Process the message Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString()); var handler = MessageHandlerFactory.GetMessageHandler(receivedMessage.ContentType); handler.Handle(receivedMessage, CompletedClient); receivedMessage.Complete(); } catch (Exception ex) { // Handle any message processing specific exceptions here } }); CompletedEvent.WaitOne(); }