-
Notifications
You must be signed in to change notification settings - Fork 437
/
Copy pathProgram.cs
215 lines (172 loc) · 9.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright © 2010-2021 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using CefSharp.OffScreen;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace CefSharp.MinimalExample.OffScreen
{
/// <summary>
/// CefSharp.OffScreen Minimal Example
/// </summary>
public static class Program
{
/// <summary>
/// Asynchronous demo using CefSharp.OffScreen
/// Loads google.com, uses javascript to fill out the search box then takes a screenshot which is opened
/// in the default image viewer.
/// For a synchronous demo see <see cref="MainSync(string[])"/> below.
/// </summary>
/// <param name="args">args</param>
/// <returns>exit code</returns>
public static int Main(string[] args)
{
#if ANYCPU
//Only required for PlatformTarget of AnyCPU
CefRuntime.SubscribeAnyCpuAssemblyResolver();
#endif
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();
//Console apps don't have a SynchronizationContext, so to ensure our await calls continue on the main thread we use a super simple implementation from
//https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/
//Continuations will happen on the main thread. Cef.Initialize/Cef.Shutdown must be called on the same Thread.
//The Nito.AsyncEx.Context Nuget package has a more advanced implementation
//should you wish to use a pre-build implementation.
//https://github.com/StephenCleary/AsyncEx/blob/8a73d0467d40ca41f9f9cf827c7a35702243abb8/doc/AsyncContext.md#console-example-using-asynccontext
//NOTE: This is only required if you use await
AsyncContext.Run(async delegate
{
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")
};
//Perform dependency check to make sure all relevant resources are in our output directory.
var success = await Cef.InitializeAsync(settings, performDependencyCheck: true, browserProcessHandler: null);
if (!success)
{
var exitCode = Cef.GetExitCode();
throw new Exception($"Cef.Initialize failed with {exitCode}, check the log file for more details.");
}
// Create the CefSharp.OffScreen.ChromiumWebBrowser instance
using (var browser = new ChromiumWebBrowser(testUrl))
{
var initialLoadResponse = await browser.WaitForInitialLoadAsync();
if (!initialLoadResponse.Success)
{
throw new Exception(string.Format("Page load failed with ErrorCode:{0}, HttpStatusCode:{1}", initialLoadResponse.ErrorCode, initialLoadResponse.HttpStatusCode));
}
_ = await browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
//Give the browser a little time to render
await Task.Delay(500);
// Wait for the screenshot to be taken.
var bitmapAsByteArray = await browser.CaptureScreenshotAsync();
// File path to save our screenshot e.g. C:\Users\{username}\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);
File.WriteAllBytes(screenshotPath, bitmapAsByteArray);
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
// Tell Windows to launch the saved image.
Process.Start(new ProcessStartInfo(screenshotPath)
{
// UseShellExecute is false by default on .NET Core.
UseShellExecute = true
});
Console.WriteLine("Image viewer launched. Press any key to exit.");
}
// Wait for user to press a key before exit
Console.ReadKey();
// Clean up Chromium objects. You need to call this in your application otherwise
// you will get a crash when closing.
Cef.Shutdown();
});
return 0;
}
/// <summary>
/// Synchronous demo using CefSharp.OffScreen
/// Loads google.com, uses javascript to fill out the search box then takes a screenshot which is opened
/// in the default image viewer.
/// For a asynchronous demo see <see cref="Main(string[])"/> above.
/// To use this demo simply delete the <see cref="Main(string[])"/> method and rename this method to Main.
/// </summary>
/// <param name="args">args</param>
/// <returns>exit code</returns>
public static int MainSync(string[] args)
{
#if ANYCPU
//Only required for PlatformTarget of AnyCPU
CefRuntime.SubscribeAnyCpuAssemblyResolver();
#endif
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();
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")
};
//Perform dependency check to make sure all relevant resources are in our output directory.
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
// Create the offscreen Chromium browser.
var browser = new ChromiumWebBrowser(testUrl);
EventHandler<LoadingStateChangedEventArgs> handler = null;
handler = (s, e) =>
{
// Check to see if loading is complete - this event is called twice, one when loading starts
// second time when it's finished
if (!e.IsLoading)
{
// Remove the load event handler, because we only want one snapshot of the page.
browser.LoadingStateChanged -= handler;
var scriptTask = browser.EvaluateScriptAsync("document.querySelector('[name=q]').value = 'CefSharp Was Here!'");
scriptTask.ContinueWith(t =>
{
if(!t.Result.Success)
{
throw new Exception("EvaluateScriptAsync failed:" + t.Result.Message);
}
//Give the browser a little time to render
Thread.Sleep(500);
// Wait for the screenshot to be taken.
var task = browser.CaptureScreenshotAsync();
task.ContinueWith(x =>
{
// File path to save our screenshot e.g. C:\Users\{username}\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);
var bitmapAsByteArray = x.Result;
// Save the Bitmap to the path.
File.WriteAllBytes(screenshotPath, bitmapAsByteArray);
Console.WriteLine("Screenshot saved. Launching your default image viewer...");
// Tell Windows to launch the saved image.
Process.Start(new ProcessStartInfo(screenshotPath)
{
// UseShellExecute is false by default on .NET Core.
UseShellExecute = true
});
Console.WriteLine("Image viewer launched. Press any key to exit.");
}, TaskScheduler.Default);
});
}
};
// An event that is fired when the first page is finished loading.
// This returns to us from another thread.
browser.LoadingStateChanged += handler;
// 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.
//The ChromiumWebBrowser instance will be disposed
Cef.Shutdown();
return 0;
}
}
}