只可能對做這個工作另一個線程使用任務。因爲帶ChromiumWebBrowser控件的主線程需要可以自由加載和呈現網頁。
下面是如何使用新的方法的例子:
// Take screenshots of 3 web pages, and save the screenshots to C:\testX.png
var takeScreenshots = GetScreenshots(Browser, new[] { "http://www.google.com/", "http://www.yahoo.com", "http://www.apple.com" });
takeScreenshots.ContinueWith((task) => {
int i = 0;
foreach (var bitmap in task.Result)
bitmap.Save(@"C:\test" + (i++).ToString() + ".png");
});
takeScreenshots.Start();
您需要將此方法添加到您的類:
public Task<List<Bitmap>> GetScreenshots(ChromiumWebBrowser browser, IEnumerable<string> urls)
{
return new Task<List<Bitmap>>(() => {
var bitmaps = new List<Bitmap>();
var screenshotDone = new AutoResetEvent(false);
EventHandler<FrameLoadEndEventArgs> loaded = null;
loaded = (s, e) => {
// Start a new thread so CefSharp can finish loading and rendering the page.
if (e.IsMainFrame && e.Url != "about:blank") {
new System.Threading.Tasks.Task(() => {
System.Threading.Thread.Sleep(3000);
bitmaps.Add(ControlSnapshot.Snapshot(browser));
screenshotDone.Set();
}).Start();
}
};
browser.FrameLoadEnd += loaded;
foreach (string url in urls) {
// Wait until page finished loading and take a screenshot.
browser.Invoke(new MethodInvoker(() => {
browser.Load(url);
}));
screenshotDone.WaitOne();
}
browser.FrameLoadEnd -= loaded;
return bitmaps;
});
}
隨着這些using
聲明:
using System.Threading;
using System.Threading.Tasks;
您還需要添加一個新的文件來創建快照,ControlSnapshot.cs
:
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ControlSnapshot
{
public static Bitmap Snapshot(Control c)
{
int width = 0, height = 0;
IntPtr hwnd = IntPtr.Zero;
IntPtr dc = IntPtr.Zero;
c.Invoke(new MethodInvoker(() => {
width = c.ClientSize.Width;
height = c.ClientSize.Height;
hwnd = c.Handle;
dc = GetDC(hwnd);
}));
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);
if (dc != IntPtr.Zero) {
try {
using (Graphics g = Graphics.FromImage(bmp)) {
IntPtr bdc = g.GetHdc();
try {
BitBlt(bdc, 0, 0, width, height, dc, 0, 0, TernaryRasterOperations.SRCCOPY);
} finally {
g.ReleaseHdc(bdc);
}
}
} finally {
ReleaseDC(hwnd, dc);
}
}
return bmp;
}
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
public enum TernaryRasterOperations : uint
{
/// <summary>dest = source</summary>
SRCCOPY = 0x00CC0020,
/// <summary>dest = source OR dest</summary>
SRCPAINT = 0x00EE0086,
/// <summary>dest = source AND dest</summary>
SRCAND = 0x008800C6,
/// <summary>dest = source XOR dest</summary>
SRCINVERT = 0x00660046,
/// <summary>dest = source AND (NOT dest)</summary>
SRCERASE = 0x00440328,
/// <summary>dest = (NOT source)</summary>
NOTSRCCOPY = 0x00330008,
/// <summary>dest = (NOT src) AND (NOT dest)</summary>
NOTSRCERASE = 0x001100A6,
/// <summary>dest = (source AND pattern)</summary>
MERGECOPY = 0x00C000CA,
/// <summary>dest = (NOT source) OR dest</summary>
MERGEPAINT = 0x00BB0226,
/// <summary>dest = pattern</summary>
PATCOPY = 0x00F00021,
/// <summary>dest = DPSnoo</summary>
PATPAINT = 0x00FB0A09,
/// <summary>dest = pattern XOR dest</summary>
PATINVERT = 0x005A0049,
/// <summary>dest = (NOT dest)</summary>
DSTINVERT = 0x00550009,
/// <summary>dest = BLACK</summary>
BLACKNESS = 0x00000042,
/// <summary>dest = WHITE</summary>
WHITENESS = 0x00FF0062,
/// <summary>
/// Capture window as seen on screen. This includes layered windows
/// such as WPF windows with AllowsTransparency="true"
/// </summary>
CAPTUREBLT = 0x40000000
}
}
你能粘貼到目前爲止的代碼嗎? – Yoshi 2014-12-04 09:18:48
你想用加載的頁面做什麼?只要看看他們X秒? CefSharp一次只能查看一個頁面,除非您在新選項卡中打開它們。 – Yoshi 2014-12-04 10:21:23
我只想通過javascript獲取一些操作的屏幕截圖。 – Sanjay 2014-12-04 13:23:58