-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathChromiumWebBrowserWithScreenshotSupport.cs
More file actions
220 lines (187 loc) · 9.07 KB
/
ChromiumWebBrowserWithScreenshotSupport.cs
File metadata and controls
220 lines (187 loc) · 9.07 KB
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
216
217
218
219
220
// Copyright © 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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Size = System.Windows.Size;
namespace CefSharp.Wpf.Example.Controls
{
/// <summary>
/// Example with Screenshot support - adapted from https://github.com/cefsharp/CefSharp/pull/462/
/// </summary>
public class ChromiumWebBrowserWithScreenshotSupport : ChromiumWebBrowser
{
[DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
private static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);
private static readonly PixelFormat PixelFormat = PixelFormats.Pbgra32;
private static readonly int BytesPerPixel = PixelFormat.BitsPerPixel / 8;
private volatile bool isTakingScreenshot = false;
private Size? screenshotSize;
private int oldFrameRate;
private int ignoreFrames = 0;
private TaskCompletionSource<InteropBitmap> screenshotTaskCompletionSource;
private CancellationTokenRegistration? cancellationTokenRegistration;
public ICommand ScreenshotCommand { get; set; }
public ChromiumWebBrowserWithScreenshotSupport() : base()
{
ScreenshotCommand = new RelayCommand(TakeScreenshot);
}
public Task<InteropBitmap> TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, CancellationToken? cancellationToken = null)
{
if (screenshotTaskCompletionSource != null && screenshotTaskCompletionSource.Task.Status == TaskStatus.Running)
{
throw new Exception("Screenshot already in progress, you must wait for the previous screenshot to complete");
}
if (IsBrowserInitialized == false)
{
throw new Exception("Browser has not yet finished initializing or is being disposed");
}
if (IsLoading)
{
throw new Exception("Unable to take screenshot while browser is loading");
}
var browserHost = this.GetBrowserHost();
if (browserHost == null)
{
throw new Exception("IBrowserHost is null");
}
screenshotTaskCompletionSource = new TaskCompletionSource<InteropBitmap>(TaskCreationOptions.RunContinuationsAsynchronously);
if (cancellationToken.HasValue)
{
var token = cancellationToken.Value;
cancellationTokenRegistration = token.Register(() =>
{
screenshotTaskCompletionSource.TrySetCanceled();
cancellationTokenRegistration?.Dispose();
}, useSynchronizationContext: false);
}
if (frameRate.HasValue)
{
oldFrameRate = browserHost.WindowlessFrameRate;
browserHost.WindowlessFrameRate = frameRate.Value;
}
this.screenshotSize = screenshotSize;
this.isTakingScreenshot = true;
this.ignoreFrames = ignoreFrames.GetValueOrDefault() < 0 ? 0 : ignoreFrames.GetValueOrDefault();
//Resize the browser using the desired screenshot dimensions
//The resulting bitmap will never be rendered to the screen
browserHost.WasResized();
return screenshotTaskCompletionSource.Task;
}
#if ! CEFSHARP_WPF_HWNDHOST
protected override CefSharp.Structs.Rect GetViewRect()
{
if (isTakingScreenshot)
{
return new CefSharp.Structs.Rect(0, 0, (int)Math.Ceiling(screenshotSize.Value.Width), (int)Math.Ceiling(screenshotSize.Value.Height));
}
return base.GetViewRect();
}
protected override void OnPaint(bool isPopup, Structs.Rect dirtyRect, IntPtr buffer, int width, int height)
{
if (isTakingScreenshot)
{
//We ignore the first n number of frames
if (ignoreFrames > 0)
{
ignoreFrames--;
return;
}
//Wait until we have a frame that matches the updated size we requested
if (screenshotSize.HasValue && screenshotSize.Value.Width == width && screenshotSize.Value.Height == height)
{
var stride = width * BytesPerPixel;
var numberOfBytes = stride * height;
//Create out own memory mapped view for the screenshot and copy the buffer into it.
//If we were going to create a lot of screenshots then it would be better to allocate a large buffer
//and reuse it.
var mappedFile = MemoryMappedFile.CreateNew(null, numberOfBytes, MemoryMappedFileAccess.ReadWrite);
var viewAccessor = mappedFile.CreateViewAccessor();
CopyMemory(viewAccessor.SafeMemoryMappedViewHandle.DangerousGetHandle(), buffer, (uint)numberOfBytes);
//Bitmaps need to be created on the UI thread
Dispatcher.BeginInvoke((Action)(() =>
{
var backBuffer = mappedFile.SafeMemoryMappedFileHandle.DangerousGetHandle();
//NOTE: Interopbitmap is not capable of supporting DPI scaling
var bitmap = (InteropBitmap)Imaging.CreateBitmapSourceFromMemorySection(backBuffer,
width, height, PixelFormat, stride, 0);
screenshotTaskCompletionSource.TrySetResult(bitmap);
isTakingScreenshot = false;
var browserHost = GetBrowser().GetHost();
//Return the framerate to the previous value
browserHost.WindowlessFrameRate = oldFrameRate;
//Let the browser know the size changes so normal rendering can continue
browserHost.WasResized();
viewAccessor?.Dispose();
mappedFile?.Dispose();
cancellationTokenRegistration?.Dispose();
}));
}
}
else
{
base.OnPaint(isPopup, dirtyRect, buffer, width, height);
}
}
#endif
private void TakeScreenshot()
{
var uiThreadTaskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
const string script = "[document.body.scrollWidth, document.body.scrollHeight]";
this.EvaluateScriptAsync(script).ContinueWith((scriptTask) =>
{
var javascriptResponse = scriptTask.Result;
if (javascriptResponse.Success)
{
var widthAndHeight = (List<object>)javascriptResponse.Result;
var screenshotSize = new Size((int)widthAndHeight[0], (int)widthAndHeight[1]);
TakeScreenshot(screenshotSize, ignoreFrames: 0).ContinueWith((screenshotTask) =>
{
if (screenshotTask.Status == TaskStatus.RanToCompletion)
{
try
{
var bitmap = screenshotTask.Result;
var tempFile = Path.GetTempFileName().Replace(".tmp", ".png");
using (var stream = new FileStream(tempFile, FileMode.Create))
{
var encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmap));
encoder.Save(stream);
}
Process.Start(new ProcessStartInfo
{
UseShellExecute = true,
FileName = tempFile
});
}
catch (Exception ex)
{
var msg = ex.ToString();
}
}
else
{
MessageBox.Show("Unable to capture screenshot");
}
}, uiThreadTaskScheduler); //Make sure continuation runs on UI thread
}
else
{
MessageBox.Show("Unable to obtain size of screenshot");
}
}, uiThreadTaskScheduler);
}
}
}