2018-09-10 02:41:13 +02:00
|
|
|
// Copyright © 2014 The CefSharp Authors. All rights reserved.
|
2014-11-14 18:48:46 +11:00
|
|
|
//
|
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
using System;
|
2014-11-14 18:19:22 +11:00
|
|
|
using System.Diagnostics;
|
2014-12-30 10:44:31 +10:00
|
|
|
using System.Drawing;
|
2014-11-14 18:19:22 +11:00
|
|
|
using System.IO;
|
2014-12-30 10:44:31 +10:00
|
|
|
using System.Threading.Tasks;
|
2018-09-17 00:46:21 +02:00
|
|
|
using CefSharp.Example;
|
|
|
|
|
using CefSharp.Example.Handlers;
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2014-11-18 13:15:34 +10:00
|
|
|
namespace CefSharp.OffScreen.Example
|
2014-11-14 18:19:22 +11:00
|
|
|
{
|
2014-11-18 13:05:31 +10:00
|
|
|
public class Program
|
2014-11-14 18:19:22 +11:00
|
|
|
{
|
2015-04-24 16:00:31 +10:00
|
|
|
private const string TestUrl = "https://www.google.com/";
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2016-12-12 18:46:11 +10:00
|
|
|
public static int Main(string[] args)
|
2014-11-14 18:19:22 +11:00
|
|
|
{
|
2015-04-24 16:00:31 +10:00
|
|
|
Console.WriteLine("This example application will load {0}, take a screenshot, and save it to your desktop.", TestUrl);
|
2014-11-14 18:19:22 +11:00
|
|
|
Console.WriteLine("You may see a lot of Chromium debugging output, please wait...");
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
2020-05-04 11:34:52 +10:00
|
|
|
Cef.EnableWaitForBrowsersToClose();
|
|
|
|
|
|
2014-11-14 18:19:22 +11:00
|
|
|
// You need to replace this with your own call to Cef.Initialize();
|
2018-08-02 10:47:12 +10:00
|
|
|
CefExample.Init(new CefSettings(), browserProcessHandler: new BrowserProcessHandler());
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2019-09-11 12:18:45 +10:00
|
|
|
MainAsync("cache\\path1", 1.0);
|
2015-07-22 14:25:10 +10:00
|
|
|
//Demo showing Zoom Level of 3.0
|
|
|
|
|
//Using seperate request contexts allows the urls from the same domain to have independent zoom levels
|
|
|
|
|
//otherwise they would be the same - default behaviour of Chromium
|
2019-09-11 12:18:45 +10:00
|
|
|
//MainAsync("cache\\path2", 3.0);
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2015-04-24 16:00:31 +10:00
|
|
|
// We have to wait for something, otherwise the process will exit too soon.
|
|
|
|
|
Console.ReadKey();
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2020-05-04 11:34:52 +10:00
|
|
|
//Wait until the browser has finished closing (which by default happens on a different thread).
|
|
|
|
|
//Cef.EnableWaitForBrowsersToClose(); must be called before Cef.Initialize to enable this feature
|
|
|
|
|
//See https://github.com/cefsharp/CefSharp/issues/3047 for details
|
|
|
|
|
Cef.WaitForBrowsersToClose();
|
|
|
|
|
|
2014-11-14 18:19:22 +11:00
|
|
|
// Clean up Chromium objects. You need to call this in your application otherwise
|
|
|
|
|
// you will get a crash when closing.
|
|
|
|
|
Cef.Shutdown();
|
2016-12-12 18:46:11 +10:00
|
|
|
|
|
|
|
|
//Success
|
|
|
|
|
return 0;
|
2014-11-14 18:19:22 +11:00
|
|
|
}
|
|
|
|
|
|
2015-07-21 23:54:17 +10:00
|
|
|
private static async void MainAsync(string cachePath, double zoomLevel)
|
2014-11-14 18:19:22 +11:00
|
|
|
{
|
2015-07-22 13:51:16 +10:00
|
|
|
var browserSettings = new BrowserSettings();
|
2015-09-10 15:36:40 +10:00
|
|
|
//Reduce rendering speed to one frame per second so it's easier to take screen shots
|
|
|
|
|
browserSettings.WindowlessFrameRate = 1;
|
2020-05-03 16:59:57 +10:00
|
|
|
var requestContextSettings = new RequestContextSettings
|
|
|
|
|
{
|
|
|
|
|
CachePath = Path.GetFullPath(cachePath)
|
|
|
|
|
};
|
2015-07-22 14:25:10 +10:00
|
|
|
|
|
|
|
|
// RequestContext can be shared between browser instances and allows for custom settings
|
|
|
|
|
// e.g. CachePath
|
2018-09-10 02:41:13 +02:00
|
|
|
using (var requestContext = new RequestContext(requestContextSettings))
|
2015-07-22 13:51:16 +10:00
|
|
|
using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
|
2014-11-14 18:19:22 +11:00
|
|
|
{
|
2015-07-22 14:25:10 +10:00
|
|
|
if (zoomLevel > 1)
|
2015-07-16 16:44:45 +02:00
|
|
|
{
|
2015-07-22 14:25:10 +10:00
|
|
|
browser.FrameLoadStart += (s, argsi) =>
|
2015-07-16 16:44:45 +02:00
|
|
|
{
|
2015-07-22 14:25:10 +10:00
|
|
|
var b = (ChromiumWebBrowser)s;
|
2015-09-01 22:48:50 +10:00
|
|
|
if (argsi.Frame.IsMain)
|
2015-07-22 14:25:10 +10:00
|
|
|
{
|
|
|
|
|
b.SetZoomLevel(zoomLevel);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2015-04-24 16:00:31 +10:00
|
|
|
await LoadPageAsync(browser);
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2016-04-27 20:55:06 +10:00
|
|
|
//Check preferences on the CEF UI Thread
|
|
|
|
|
await Cef.UIThreadTaskFactory.StartNew(delegate
|
|
|
|
|
{
|
|
|
|
|
var preferences = requestContext.GetAllPreferences(true);
|
|
|
|
|
|
|
|
|
|
//Check do not track status
|
|
|
|
|
var doNotTrack = (bool)preferences["enable_do_not_track"];
|
|
|
|
|
|
2019-01-10 23:57:28 +01:00
|
|
|
Debug.WriteLine("DoNotTrack: " + doNotTrack);
|
2016-04-27 20:55:06 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var onUi = Cef.CurrentlyOnThread(CefThreadIds.TID_UI);
|
2015-11-27 12:18:56 +10:00
|
|
|
|
2015-09-10 15:36:40 +10:00
|
|
|
// For Google.com pre-pupulate the search text box
|
2020-02-18 21:53:27 +01:00
|
|
|
await browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
|
2015-09-10 15:36:40 +10:00
|
|
|
|
2017-09-15 13:34:02 +10:00
|
|
|
//Example using SendKeyEvent for input instead of javascript
|
|
|
|
|
//var browserHost = browser.GetBrowserHost();
|
|
|
|
|
//var inputString = "CefSharp Was Here!";
|
|
|
|
|
//foreach(var c in inputString)
|
|
|
|
|
//{
|
|
|
|
|
// browserHost.SendKeyEvent(new KeyEvent { WindowsKeyCode = c, Type = KeyEventType.Char });
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
////Give the browser a little time to finish drawing our SendKeyEvent input
|
|
|
|
|
//await Task.Delay(100);
|
|
|
|
|
|
2015-09-16 08:41:42 +10:00
|
|
|
// Wait for the screenshot to be taken,
|
|
|
|
|
// if one exists ignore it, wait for a new one to make sure we have the most up to date
|
2015-09-10 15:36:40 +10:00
|
|
|
await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
|
2015-04-24 16:00:31 +10:00
|
|
|
|
|
|
|
|
await LoadPageAsync(browser, "http://github.com");
|
2018-09-10 02:41:13 +02:00
|
|
|
|
2015-09-16 08:41:42 +10:00
|
|
|
//Gets a wrapper around the underlying CefBrowser instance
|
|
|
|
|
var cefBrowser = browser.GetBrowser();
|
|
|
|
|
// Gets a warpper around the CefBrowserHost instance
|
|
|
|
|
// You can perform a lot of low level browser operations using this interface
|
|
|
|
|
var cefHost = cefBrowser.GetHost();
|
|
|
|
|
|
|
|
|
|
//You can call Invalidate to redraw/refresh the image
|
|
|
|
|
cefHost.Invalidate(PaintElementType.View);
|
|
|
|
|
|
2015-04-24 16:00:31 +10:00
|
|
|
// Wait for the screenshot to be taken.
|
2015-09-16 08:41:42 +10:00
|
|
|
await browser.ScreenshotAsync(true).ContinueWith(DisplayBitmap);
|
2015-04-24 16:00:31 +10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Task LoadPageAsync(IWebBrowser browser, string address = null)
|
|
|
|
|
{
|
2018-04-12 17:59:23 +10:00
|
|
|
var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
|
2015-04-24 16:00:31 +10:00
|
|
|
|
|
|
|
|
EventHandler<LoadingStateChangedEventArgs> handler = null;
|
2015-05-20 18:03:08 +10:00
|
|
|
handler = (sender, args) =>
|
2015-04-24 16:00:31 +10:00
|
|
|
{
|
|
|
|
|
//Wait for while page to finish loading not just the first frame
|
|
|
|
|
if (!args.IsLoading)
|
|
|
|
|
{
|
|
|
|
|
browser.LoadingStateChanged -= handler;
|
2018-04-12 17:59:23 +10:00
|
|
|
//Important that the continuation runs async using TaskCreationOptions.RunContinuationsAsynchronously
|
|
|
|
|
tcs.TrySetResult(true);
|
2015-04-24 16:00:31 +10:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
browser.LoadingStateChanged += handler;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(address))
|
|
|
|
|
{
|
|
|
|
|
browser.Load(address);
|
2014-12-30 10:44:31 +10:00
|
|
|
}
|
2015-04-24 16:00:31 +10:00
|
|
|
return tcs.Task;
|
2014-12-30 10:44:31 +10:00
|
|
|
}
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2014-12-30 10:44:31 +10:00
|
|
|
private static void DisplayBitmap(Task<Bitmap> task)
|
|
|
|
|
{
|
|
|
|
|
// Make a file to save it to (e.g. C:\Users\jan\Desktop\CefSharp screenshot.png)
|
2015-04-24 16:00:31 +10:00
|
|
|
var screenshotPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "CefSharp screenshot" + DateTime.Now.Ticks + ".png");
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2014-12-30 10:44:31 +10:00
|
|
|
Console.WriteLine();
|
|
|
|
|
Console.WriteLine("Screenshot ready. Saving to {0}", screenshotPath);
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2014-12-30 10:44:31 +10:00
|
|
|
var bitmap = task.Result;
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2014-12-30 10:44:31 +10:00
|
|
|
// Save the Bitmap to the path.
|
|
|
|
|
// The image type is auto-detected via the ".png" extension.
|
|
|
|
|
bitmap.Save(screenshotPath);
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2014-12-30 10:44:31 +10:00
|
|
|
// We no longer need the Bitmap.
|
|
|
|
|
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
|
|
|
|
|
bitmap.Dispose();
|
2014-11-14 18:19:22 +11:00
|
|
|
|
2020-07-18 13:30:54 +10:00
|
|
|
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
|
2014-12-30 10:44:31 +10:00
|
|
|
|
|
|
|
|
// Tell Windows to launch the saved image.
|
2020-07-18 13:30:54 +10:00
|
|
|
Process.Start(new ProcessStartInfo(screenshotPath)
|
|
|
|
|
{
|
|
|
|
|
// UseShellExecute is false by default on .NET Core.
|
|
|
|
|
UseShellExecute = true
|
|
|
|
|
});
|
2014-12-30 10:44:31 +10:00
|
|
|
|
|
|
|
|
Console.WriteLine("Image viewer launched. Press any key to exit.");
|
2014-11-14 18:19:22 +11:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|