2012-12-21 64 views
0

林:如何保存WebBrowser頁面url內容每秒的圖像?使用此代碼

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Net; 
using System.Drawing.Imaging; 
namespace DownloadFlashFiles 
{ 
    public partial class Form1 : Form 
    { 
     int i = 0; 
     public Form1() 
     { 
      InitializeComponent(); 
      GenerateScreenshot("http://www.nana10.co.il"); 


     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
     } 
     public Bitmap GenerateScreenshot(string url) 
     { 
      // This method gets a screenshot of the webpage 
      // rendered at its full size (height and width) 
      return GenerateScreenshot(url, -1, -1); 
     } 
     public Bitmap GenerateScreenshot(string url, int width, int height) 
     { 
      // Load the webpage into a WebBrowser control 
      //WebBrowser wb = new WebBrowser(); 
      webBrowser1.ScrollBarsEnabled = false; 
      webBrowser1.ScriptErrorsSuppressed = true; 
      webBrowser1.Navigate(url); 
      while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); } 
      // Set the size of the WebBrowser control 
      webBrowser1.Width = width; 
      webBrowser1.Height = height; 
      if (width == -1) 
      { 
       // Take Screenshot of the web pages full width 
       webBrowser1.Width = webBrowser1.Document.Body.ScrollRectangle.Width; 
      } 
      if (height == -1) 
      { 
       // Take Screenshot of the web pages full height 
       webBrowser1.Height = webBrowser1.Document.Body.ScrollRectangle.Height; 
      } 
      // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control 
      Bitmap bitmap = new Bitmap(webBrowser1.Width, webBrowser1.Height); 
      webBrowser1.DrawToBitmap(bitmap, new Rectangle(0, 0, webBrowser1.Width, webBrowser1.Height)); 
      //webBrowser1.Dispose(); 
      return bitmap; 
     } 
     private void timer1_Tick(object sender, EventArgs e) 
     { 
      i++; 
      Bitmap thumbnail = GenerateScreenshot("http://www.nana10.co.il"); 
      thumbnail.Save(@"d:\test4\" + i.ToString("D6") + "bmp", ImageFormat.Bmp); 
     } 
     private void button1_Click(object sender, EventArgs e) 
     { 
      timer1.Start(); 
     } 

    } 
} 

問題是,當IM點擊按鈕,我看到裝在WerBrowser內容每一秒,但它從來沒有到Timer1 Tick事件裏面的代碼,因爲它停留在當循環這一行:

while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }

我要的是加載新的內容OT了web瀏覽器每一秒,也節省從網頁瀏覽器每秒向硬盤中的新內容。

我是如何做到這一點,因爲它是在While循環中?

回答

相關問題