2009-04-22 139 views
2

使用WatiN捕捉圖像時,生成的圖像僅爲空白,爲純黑色。不過,圖像的大小等於屏幕大小。例如,以下代碼片段僅保存兩張黑色圖片。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

希望任何可以闡明這一點。

乾杯 //約翰

+0

您正在使用哪種版本的IE和WatiN? – 2009-04-24 13:05:42

回答

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並呼籲它。