2

使用 WatiN 捕获图像时,生成的图像只是空的,纯黑色。但是,图像的大小等于屏幕大小。例如,下面的代码片段只保存了两个黑色图像。

using (IE ie = new IE()) {
            ie.ClearCache();
            ie.BringToFront();
            ie.GoTo("http://localhost/");
            ie.CaptureWebPageToFile(imageDir + "\\localhost.png");
            WatiN.Core.CaptureWebPage capture = new CaptureWebPage(ie);
            capture.CaptureWebPageToFile(imageDir + "\\localhost.jpg", true, true, 100, 80);
            Assert.IsTrue(ie.ContainsText("Zend"));
        }

其他人也报告了这一点,但我还没有看到任何解决方案。请参阅此处的评论:http: //www.codeproject.com/KB/graphics/IECapture.aspx?display= PrintAll&fid=192174&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=51&select=1810490

希望任何人都可以对此有所了解。

干杯//约翰

4

1 回答 1

3

通过以下更改,我设法让它在 IE8 下为我的网页工作:

替换以下方法:

private static IntPtr GetHwndForInternetExplorerServer(IntPtr hwnd)
{
    var sbc = new StringBuilder(256);
    hwnd = NativeMethods.GetWindow(hwnd, NativeMethods.GW_CHILD);
    while (hwnd != IntPtr.Zero)
    {
        NativeMethods.GetClassName(hwnd, sbc, 256);
        if (sbc.ToString().IndexOf("Shell DocObject View", 0) > -1) //IE6
        {
            hwnd = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
            break;
        }
        if (sbc.ToString().IndexOf("TabWindowClass", 0) > -1) //IE7
        {
            hwnd = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero);
            hwnd = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
            break;
        }
        if (sbc.ToString().IndexOf("Frame Tab", 0) > -1) // IE8
        {
            hwnd = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "TabWindowClass", IntPtr.Zero);
            hwnd = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "Shell DocObject View", IntPtr.Zero);
            hwnd = NativeMethods.FindWindowEx(hwnd, IntPtr.Zero, "Internet Explorer_Server", IntPtr.Zero);
            break;
        }
        hwnd = NativeMethods.GetWindow(hwnd, NativeMethods.GW_HWNDNEXT);
    }
    return hwnd;
}

删除方法 GetHwndContainingAShellDocObjectView 和对它的调用。

于 2010-01-04T09:58:43.160 回答