FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
CefSharp/CefSharp.OffScreen.Example/Program.cs at cefsharp/63 · cefsharp/CefSharp · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cefsharp
/
CefSharp
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
2.9k
Star
10.2k
Code
Issues
61
Pull requests
13
Discussions
Actions
Wiki
Security and quality
9
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
CefSharp
/
CefSharp.OffScreen.Example
/
Program.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
175 lines (140 loc) · 7.13 KB
Breadcrumbs
CefSharp
/
CefSharp.OffScreen.Example
/
Program.cs
Copy path
File metadata and controls
175 lines (140 loc) · 7.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Copyright © 2010-2017 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
System
;
using
System
.
Diagnostics
;
using
System
.
Drawing
;
using
System
.
IO
;
using
System
.
Linq
;
using
System
.
Threading
.
Tasks
;
using
CefSharp
;
using
CefSharp
.
Example
;
using
CefSharp
.
Example
.
Handlers
;
using
CefSharp
.
Internals
;
namespace
CefSharp
.
OffScreen
.
Example
{
public
class
Program
{
private
const
string
TestUrl
=
"https://www.google.com/"
;
public
static
int
Main
(
string
[
]
args
)
{
Console
.
WriteLine
(
"This example application will load {0}, take a screenshot, and save it to your desktop."
,
TestUrl
)
;
Console
.
WriteLine
(
"You may see a lot of Chromium debugging output, please wait..."
)
;
Console
.
WriteLine
(
)
;
// You need to replace this with your own call to Cef.Initialize();
CefExample
.
Init
(
true
,
multiThreadedMessageLoop
:
true
,
browserProcessHandler
:
new
BrowserProcessHandler
(
)
)
;
MainAsync
(
"cachePath1"
,
1.0
)
;
//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
//MainAsync("cachePath2", 3.0);
// 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
(
)
;
//Success
return
0
;
}
private
static
async
void
MainAsync
(
string
cachePath
,
double
zoomLevel
)
{
var
browserSettings
=
new
BrowserSettings
(
)
;
//Reduce rendering speed to one frame per second so it's easier to take screen shots
browserSettings
.
WindowlessFrameRate
=
1
;
var
requestContextSettings
=
new
RequestContextSettings
{
CachePath
=
cachePath
}
;
// RequestContext can be shared between browser instances and allows for custom settings
// e.g. CachePath
using
(
var
requestContext
=
new
RequestContext
(
requestContextSettings
)
)
using
(
var
browser
=
new
ChromiumWebBrowser
(
TestUrl
,
browserSettings
,
requestContext
)
)
{
if
(
zoomLevel
>
1
)
{
browser
.
FrameLoadStart
+=
(
s
,
argsi
)
=>
{
var
b
=
(
ChromiumWebBrowser
)
s
;
if
(
argsi
.
Frame
.
IsMain
)
{
b
.
SetZoomLevel
(
zoomLevel
)
;
}
}
;
}
await
LoadPageAsync
(
browser
)
;
//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"
]
;
Debug
.
WriteLine
(
"DoNotTrack:"
+
doNotTrack
)
;
}
)
;
var
onUi
=
Cef
.
CurrentlyOnThread
(
CefThreadIds
.
TID_UI
)
;
// For Google.com pre-pupulate the search text box
await
browser
.
EvaluateScriptAsync
(
"document.getElementById('lst-ib').value = 'CefSharp Was Here!'"
)
;
//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);
// 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
await
browser
.
ScreenshotAsync
(
true
)
.
ContinueWith
(
DisplayBitmap
)
;
await
LoadPageAsync
(
browser
,
"http://github.com"
)
;
//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
)
;
// Wait for the screenshot to be taken.
await
browser
.
ScreenshotAsync
(
true
)
.
ContinueWith
(
DisplayBitmap
)
;
}
}
public
static
Task
LoadPageAsync
(
IWebBrowser
browser
,
string
address
=
null
)
{
//If using .Net 4.6 then use TaskCreationOptions.RunContinuationsAsynchronously
//and switch to tcs.TrySetResult below - no need for the custom extension method
var
tcs
=
new
TaskCompletionSource
<
bool
>
(
)
;
EventHandler
<
LoadingStateChangedEventArgs
>
handler
=
null
;
handler
=
(
sender
,
args
)
=>
{
//Wait for while page to finish loading not just the first frame
if
(
!
args
.
IsLoading
)
{
browser
.
LoadingStateChanged
-=
handler
;
//This is required when using a standard TaskCompletionSource
//Extension method found in the CefSharp.Internals namespace
tcs
.
TrySetResultAsync
(
true
)
;
}
}
;
browser
.
LoadingStateChanged
+=
handler
;
if
(
!
string
.
IsNullOrEmpty
(
address
)
)
{
browser
.
Load
(
address
)
;
}
return
tcs
.
Task
;
}
private
static
void
DisplayBitmap
(
Task
<
Bitmap
>
task
)
{
// 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"
+
DateTime
.
Now
.
Ticks
+
".png"
)
;
Console
.
WriteLine
(
)
;
Console
.
WriteLine
(
"Screenshot ready. Saving to {0}"
,
screenshotPath
)
;
var
bitmap
=
task
.
Result
;
// Save the Bitmap to the path.
// The image type is auto-detected via the ".png" extension.
bitmap
.
Save
(
screenshotPath
)
;
// We no longer need the Bitmap.
// Dispose it to avoid keeping the memory alive. Especially important in 32-bit applications.
bitmap
.
Dispose
(
)
;
Console
.
WriteLine
(
"Screenshot saved. Launching your default image viewer..."
)
;
// Tell Windows to launch the saved image.
Process
.
Start
(
screenshotPath
)
;
Console
.
WriteLine
(
"Image viewer launched. Press any key to exit."
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL