public ScreenshotAsync ( bool ignoreExistingScreenshot = false, PopupBlending blend = PopupBlending.Main ) : Task |
||
ignoreExistingScreenshot | bool | Ignore existing bitmap (if any) and return the next avaliable bitmap |
blend | PopupBlending | Choose which bitmap to retrieve, choose |
return | Task |
public void TakeScreenshotOffscreen(string url, string filename) { var offscreenBrowser = new CefSharp.OffScreen.ChromiumWebBrowser(); offscreenBrowser.BrowserInitialized += (o, args) => { offscreenBrowser.Load(url); offscreenBrowser.FrameLoadEnd += async(sender1, eventArgs) => { if (eventArgs.Frame.IsMain) { var sizes = await offscreenBrowser.EvaluateScriptAsync(JavascriptQueries.RequestForMaxSize); var widthAndHeight = (List <object>)sizes.Result; var screenshotSize = new System.Drawing.Size((int)widthAndHeight[0], (int)widthAndHeight[1]); offscreenBrowser.Size = screenshotSize; await Task.Delay(500); var bitmap = await offscreenBrowser.ScreenshotAsync(false, PopupBlending.Main); bitmap.Save(filename, ImageFormat.Png); offscreenBrowser.Dispose(); Process.Start(new System.Diagnostics.ProcessStartInfo { UseShellExecute = true, FileName = filename }); } }; }; }
/// <summary> /// Get a bitmap of the specified HELM in the specified size /// </summary> /// <param name="helm"></param> /// <param name="size"></param> /// <returns></returns> public Bitmap GetBitmap( string helm, Size size) { Bitmap Bitmap = null; ManualResetEvent GetBitmapEvent = new ManualResetEvent(false); Initialize(); if (Debug) { DebugLog.Message("GetBitmap Entered: " + helm + ", " + size); } Stopwatch sw = Stopwatch.StartNew(); Helm = helm; CurrentSize = size; //if (DebugMx.True) return new Bitmap(1,1); int initialOnPaintCount = OnPaintCount; // save current paint count so we can tell when page has been re-rendered with new image string script = ""; if (!String.IsNullOrWhiteSpace(helm)) { script += "jsd.setHelm(\"" + helm + "\");"; } if (!size.IsEmpty) { script += "jsd.setSize(" + size.Width + "," + size.Height + ");"; } script += "jsd.refresh();"; // redraws the structure Task <JavascriptResponse> scriptTask = OffScreenBrowser.EvaluateScriptAsync(script); // start the script async // Wait for script completion and then get the screenshot and bitmap scriptTask.ContinueWith(t => { while (OnPaintCount == initialOnPaintCount) // wait until paint event occurs that renders the new bitmap { Thread.Sleep(10); } //Thread.Sleep(2000); //Give the browser a little time to render var task = OffScreenBrowser.ScreenshotAsync(ignoreExistingScreenshot: false); // start the screenshot async, should get new bitmap // Wait for screenshot completion and get bitmap task.ContinueWith(x => { Bitmap resultBitmap = task.Result; if (!size.IsEmpty) { Rectangle section = new Rectangle(0, 0, size.Width, size.Height); section.Intersect(new Rectangle(0, 0, resultBitmap.Width, resultBitmap.Height)); // be sure section not larger than result if (section.Height > 0 && section.Width > 0) { Bitmap = resultBitmap.Clone(section, resultBitmap.PixelFormat); } else { Bitmap = new Bitmap(1, 1); // create minimal empty bitmap } } else { Bitmap = new Bitmap(resultBitmap); } // We no longer need the Bitmap. // Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications. resultBitmap.Dispose(); GetBitmapEvent.Set(); }, TaskScheduler.Default); }); GetBitmapEvent.WaitOne(); // wait for the bitmap to be retrieved GetBitmapCount++; if (Debug) { DebugLog.StopwatchMessage("GetBitmap Complete, Time: ", sw); } return(Bitmap); }