Update OffScreen example to evaluate some javascript before taking the screenshot.
Still need to investigate a better method of waiting for render to complete as Thread.Sleep is crude Conflicts: CefSharp.MinimalExample.OffScreen/Program.cs
This commit is contained in:
parent
36a9a38008
commit
db4db05662
@ -5,6 +5,7 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using CefSharp.OffScreen;
|
||||
|
||||
namespace CefSharp.MinimalExample.OffScreen
|
||||
@ -28,7 +29,7 @@ namespace CefSharp.MinimalExample.OffScreen
|
||||
|
||||
// An event that is fired when the first page is finished loading.
|
||||
// This returns to us from another thread.
|
||||
browser.FrameLoadEnd += BrowserFrameLoadEnd;
|
||||
browser.LoadingStateChanged += BrowserLoadingStateChanged;
|
||||
|
||||
// We have to wait for something, otherwise the process will exit too soon.
|
||||
Console.ReadKey();
|
||||
@ -38,39 +39,48 @@ namespace CefSharp.MinimalExample.OffScreen
|
||||
Cef.Shutdown();
|
||||
}
|
||||
|
||||
private static void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
|
||||
private static void BrowserLoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
|
||||
{
|
||||
// Check to ensure it is the main frame which has finished loading
|
||||
// Check to see if loading is complete - this event is called twice, one when loading starts
|
||||
// second time when it's finished
|
||||
// (rather than an iframe within the main frame).
|
||||
if (e.Frame.IsMain)
|
||||
if (!e.IsLoading)
|
||||
{
|
||||
// Remove the load event handler, because we only want one snapshot of the initial page.
|
||||
browser.FrameLoadEnd -= BrowserFrameLoadEnd;
|
||||
browser.LoadingStateChanged -= BrowserLoadingStateChanged;
|
||||
|
||||
// Wait for the screenshot to be taken.
|
||||
var task = browser.ScreenshotAsync();
|
||||
task.Wait();
|
||||
var scriptTask = browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");
|
||||
|
||||
// Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
|
||||
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
|
||||
scriptTask.ContinueWith(t =>
|
||||
{
|
||||
//Give the browser a little time to render
|
||||
Thread.Sleep(500);
|
||||
// Wait for the screenshot to be taken.
|
||||
var task = browser.ScreenshotAsync();
|
||||
task.ContinueWith(x =>
|
||||
{
|
||||
// Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
|
||||
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
|
||||
|
||||
// Save the Bitmap to the path.
|
||||
// The image type is auto-detected via the ".png" extension.
|
||||
task.Result.Save(screenshotPath);
|
||||
// Save the Bitmap to the path.
|
||||
// The image type is auto-detected via the ".png" extension.
|
||||
task.Result.Save(screenshotPath);
|
||||
|
||||
// We no longer need the Bitmap.
|
||||
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
|
||||
task.Result.Dispose();
|
||||
// We no longer need the Bitmap.
|
||||
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
|
||||
task.Result.Dispose();
|
||||
|
||||
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
|
||||
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
|
||||
|
||||
// Tell Windows to launch the saved image.
|
||||
Process.Start(screenshotPath);
|
||||
// Tell Windows to launch the saved image.
|
||||
Process.Start(screenshotPath);
|
||||
|
||||
Console.WriteLine("Image viewer launched. Press any key to exit.");
|
||||
Console.WriteLine("Image viewer launched. Press any key to exit.");
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user