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:
amaitland 2015-09-10 14:59:50 +10:00
parent 36a9a38008
commit db4db05662

View File

@ -5,6 +5,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading;
using CefSharp.OffScreen; using CefSharp.OffScreen;
namespace CefSharp.MinimalExample.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. // An event that is fired when the first page is finished loading.
// This returns to us from another thread. // 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. // We have to wait for something, otherwise the process will exit too soon.
Console.ReadKey(); Console.ReadKey();
@ -38,19 +39,26 @@ namespace CefSharp.MinimalExample.OffScreen
Cef.Shutdown(); 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). // (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. // Remove the load event handler, because we only want one snapshot of the initial page.
browser.FrameLoadEnd -= BrowserFrameLoadEnd; browser.LoadingStateChanged -= BrowserLoadingStateChanged;
var scriptTask = browser.EvaluateScriptAsync("document.getElementById('lst-ib').value = 'CefSharp Was Here!'");
scriptTask.ContinueWith(t =>
{
//Give the browser a little time to render
Thread.Sleep(500);
// Wait for the screenshot to be taken. // Wait for the screenshot to be taken.
var task = browser.ScreenshotAsync(); var task = browser.ScreenshotAsync();
task.Wait(); task.ContinueWith(x =>
{
// Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png) // 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"); var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot.png");
@ -71,6 +79,8 @@ namespace CefSharp.MinimalExample.OffScreen
Process.Start(screenshotPath); Process.Start(screenshotPath);
Console.WriteLine("Image viewer launched. Press any key to exit."); Console.WriteLine("Image viewer launched. Press any key to exit.");
});
});
} }
} }
} }