2016-04-26 86 views
0

我在我的窗體中使用Web瀏覽器導航到多個頁面。我想要完整地加載完最後一頁的屏幕截圖。我編寫了webBrowser1_DocumentCompleted內的屏幕截圖。然而,在頁面加載之前,它會採取屏幕截圖。我很困惑我可以截屏的地方。我在谷歌找到LoadCompleted事件,但不知道如何使用。請幫助我。WebBrowser控制負載完成

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Web; 
using System.Threading; 
using System.Diagnostics; 
using System.Windows.Forms; 
using System.Drawing.Imaging; 

namespace CC 
{ 
    public partial class Form1 : Form 
    { 



     public Form1() 
     { 
      InitializeComponent(); 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      try 
      { 

       string url = "http://www.lll.com"; 
       webBrowser1.Navigate(new Uri(url)); 
       webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted); 

      } 
      catch (Exception ex) 
      { 

      } 
     } 

     private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      string AbsoluteURL = e.Url.AbsolutePath.ToString(); 
      int count = 0; 



      switch (AbsoluteURL) 
      { 
       case "Page1": 
        var loginControl = webBrowser1.Document.GetElementById("user"); 
        var passwordControl = webBrowser1.Document.GetElementById("password"); 
        var btn = webBrowser1.Document.GetElementById("Submit"); 

        if (loginControl != null) 
         loginControl.SetAttribute("value", "XXX"); 

        if (passwordControl != null) 
         passwordControl.SetAttribute("value", "YYY"); 

        var elems = webBrowser1.Document.GetElementsByTagName("input"); 
        foreach (HtmlElement elem in elems) 
        { 
         if (elem.GetAttribute("tabindex") == "3") 
         { 
          elem.InvokeMember("click"); 
         } 
        } 
        break; 


    case"Page2": // THIS IS NOT WORKING. TAKING SCREEN SHOT BEFORE PAGE GETTING LOADED FULLY. NEED HELP HERE 
        using (Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height, PixelFormat.Format24bppRgb)) 
     using (Graphics graphics = Graphics.FromImage(bitmap)) 
     { 
      graphics.CopyFromScreen(
       PointToScreen(webBrowser1.Location), 
       new Point(0, 0), 
       webBrowser1.Size); 
      bitmap.Save(@"C:\123.bmp"); 
     } 
        break; 
       default: 
        Console.WriteLine("Better try again"); 
        break; 


      } 

     } 



     } 


    } 
} 

回答

0

試試這個。

protected void Capture(object sender, EventArgs e) 
 
{ 
 
    string url = "www.google.com"; 
 
    Thread thread = new Thread(delegate() 
 
    { 
 
     using (WebBrowser browser = new WebBrowser()) 
 
     { 
 
      browser.ScrollBarsEnabled = false; 
 
      browser.AllowNavigation = true; 
 
      browser.Navigate(url); 
 
      browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DocumentCompleted); 
 
      while (browser.ReadyState != WebBrowserReadyState.Complete) 
 
      { 
 
       System.Windows.Forms.Application.DoEvents(); 
 
      } 
 
     } 
 
    }); 
 
    thread.SetApartmentState(ApartmentState.STA); 
 
    thread.Start(); 
 
    thread.Join(); 
 
} 
 
    
 
private void DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
 
{ 
 
    WebBrowser browser = sender as WebBrowser; 
 
     //do what do you want 
 
}

+0

謝謝拉傑什!我正在尋找一個使用Windows窗體的Webbrowser控件的解決方案。 ..我試圖在控制中完成..我相信你沒有使用控件...一天結束我想截圖我的網站一旦完全加載。 – Prabhu