Files
cefsharp/CefSharp.Wpf.Example/Controls/ChromiumWebBrowserWithScreenshotSupport.cs
T
amaitland 127c00554f Rewrite WPF/OffScreen OnPaint
OffScreen
  - Now maintains a raw Byte[] buffer that is used to create the GDI bitmaps
can be overridden and raw buffer access granted (eliminates the forced memory copy to the memory mapped view)

WPF
  - MemoryMappedFile.CreateNew is now used to create the in memory view and copy the buffer
  - InteropBitmapFactory only create new MemoryMappedFile selectively. If the MemoryMappedFile is larger than we require then just reuse it rather than
    releasing then allocating a smaller section of memory that's used for double buffering with InteropBitmap and WritableBitmap
  - Removed CefDirtyRect and used existing Rect class
  - Rename rendering event to Paint, still preferable to implement IBitmapFactory or subclass ChromiumWebBrowser
  - Update ChromiumWebBrowserWithScreenshotSupport to support new rendering method - not very well tested.

Resolves #2237
2018-01-18 11:29:12 +10:00

220 lines
8.9 KiB
C#

// 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.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Runtime.InteropServices;
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 CefSharp;
using CefSharp.Internals;
using GalaSoft.MvvmLight.Command;
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.Bgra32;
private static int BytesPerPixel = PixelFormat.BitsPerPixel / 8;
private volatile bool isTakingScreenshot = false;
private Size? screenshotSize;
private int oldFrameRate;
private int ignoreFrames = 0;
private TaskCompletionSource<InteropBitmap> screenshotTaskCompletionSource;
public ICommand ScreenshotCommand { get; set; }
public ChromiumWebBrowserWithScreenshotSupport() : base()
{
ScreenshotCommand = new RelayCommand(TakeScreenshot);
}
public Task<InteropBitmap> TakeScreenshot(Size screenshotSize, int? frameRate = 1, int? ignoreFrames = 0, TimeSpan? timeout = 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.GetBrowser().GetHost();
if(browserHost == null)
{
throw new Exception("IBrowserHost is null");
}
screenshotTaskCompletionSource = new TaskCompletionSource<InteropBitmap>();
if(timeout.HasValue)
{
screenshotTaskCompletionSource = screenshotTaskCompletionSource.WithTimeout(timeout.Value);
}
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;
}
protected override ViewRect GetViewRect()
{
if(isTakingScreenshot)
{
return new ViewRect((int)Math.Ceiling(screenshotSize.Value.Width), (int)Math.Ceiling(screenshotSize.Value.Height));
}
return base.GetViewRect();
}
protected override void OnPaint(bool isPopup, 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, PixelFormats.Bgra32, stride, 0);
//Using TaskExtensions.TrySetResultAsync extension method so continuation runs on Threadpool
screenshotTaskCompletionSource.TrySetResultAsync(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();
if(viewAccessor != null)
{
viewAccessor.Dispose();
}
if(mappedFile != null)
{
mappedFile.Dispose();
}
}));
}
}
else
{
base.OnPaint(isPopup, dirtyRect, buffer, width, height);
}
}
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 System.Diagnostics.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);
}
}
}