2015-06-15 16:33:22 +08:00
// Copyright © 2010-2015 The CefSharp Authors. All rights reserved.
2014-11-27 14:38:45 +08:00
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System ;
using System.Diagnostics ;
using System.IO ;
2015-09-10 12:59:50 +08:00
using System.Threading ;
2014-11-27 14:38:45 +08:00
using CefSharp.OffScreen ;
2016-07-15 06:17:47 +08:00
using System.Threading.Tasks ;
2014-11-27 14:38:45 +08:00
namespace CefSharp.MinimalExample.OffScreen
{
public class Program
{
private static ChromiumWebBrowser browser ;
public static void Main ( string [ ] args )
{
const string testUrl = "https://www.google.com/" ;
Console . WriteLine ( "This example application will load {0}, take a screenshot, and save it to your desktop." , testUrl ) ;
Console . WriteLine ( "You may see Chromium debugging output, please wait..." ) ;
Console . WriteLine ( ) ;
2017-03-22 13:04:33 +08:00
var settings = new CefSettings ( )
{
//By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
CachePath = Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . LocalApplicationData ) , "CefSharp\\Cache" )
} ;
2015-11-05 07:13:24 +08:00
//Perform dependency check to make sure all relevant resources are in our output directory.
2017-03-22 13:04:33 +08:00
Cef . Initialize ( settings , performDependencyCheck : true , browserProcessHandler : null ) ;
2014-11-27 14:38:45 +08:00
// Create the offscreen Chromium browser.
2015-02-27 12:04:50 +08:00
browser = new ChromiumWebBrowser ( testUrl ) ;
2014-11-27 14:38:45 +08:00
// An event that is fired when the first page is finished loading.
// This returns to us from another thread.
2015-09-10 12:59:50 +08:00
browser . LoadingStateChanged + = BrowserLoadingStateChanged ;
2014-11-27 14:38:45 +08:00
// We have to wait for something, otherwise the process will exit too soon.
Console . ReadKey ( ) ;
// Clean up Chromium objects. You need to call this in your application otherwise
// you will get a crash when closing.
Cef . Shutdown ( ) ;
}
2018-10-11 15:11:06 +08:00
private static async void BrowserLoadingStateChanged ( object sender , LoadingStateChangedEventArgs e )
2014-11-27 14:38:45 +08:00
{
2015-09-10 12:59:50 +08:00
// Check to see if loading is complete - this event is called twice, one when loading starts
// second time when it's finished
2014-11-27 14:38:45 +08:00
// (rather than an iframe within the main frame).
2015-09-10 12:59:50 +08:00
if ( ! e . IsLoading )
2014-11-27 14:38:45 +08:00
{
// Remove the load event handler, because we only want one snapshot of the initial page.
2015-09-10 12:59:50 +08:00
browser . LoadingStateChanged - = BrowserLoadingStateChanged ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
const string script = "(function(){ var elm = document.getElementById('lst-ib'); elm.value = 'CefSharp Was Here!'; return document.getElementsByName('btnK')[0].getBoundingClientRect(); })();" ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
var jsResponse = await browser . EvaluateScriptAsync ( script ) ;
if ( jsResponse . Success )
2015-09-10 12:59:50 +08:00
{
2018-10-11 15:11:06 +08:00
dynamic domRect = jsResponse . Result ;
var x = ( int ) ( domRect . x + ( domRect . width / 2 ) ) ;
var y = ( int ) ( domRect . y + ( domRect . height / 2 ) ) ;
var host = browser . GetBrowserHost ( ) ;
host . SendMouseClickEvent ( x , y , MouseButtonType . Left , false , 1 , CefEventFlags . None ) ;
Thread . Sleep ( 15 ) ;
host . SendMouseClickEvent ( x , y , MouseButtonType . Left , true , 1 , CefEventFlags . None ) ;
2015-09-10 12:59:50 +08:00
//Give the browser a little time to render
2018-10-11 15:11:06 +08:00
Thread . Sleep ( 2500 ) ;
2015-09-10 12:59:50 +08:00
// Wait for the screenshot to be taken.
2018-10-11 15:11:06 +08:00
var bitmap = await browser . ScreenshotAsync ( ) ;
// 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" ) ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
Console . WriteLine ( ) ;
Console . WriteLine ( "Screenshot ready. Saving to {0}" , screenshotPath ) ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
bitmap . Save ( screenshotPath ) ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08: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-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
Console . WriteLine ( "Screenshot saved. Launching your default image viewer..." ) ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
// Tell Windows to launch the saved image.
Process . Start ( screenshotPath ) ;
2014-11-27 14:38:45 +08:00
2018-10-11 15:11:06 +08:00
Console . WriteLine ( "Image viewer launched. Press any key to exit." ) ;
}
2014-11-27 14:38:45 +08:00
}
}
}
}