我使用这段代码:
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();
}
}
}
问题是当我单击按钮时,我看到 WerBrowser 中的内容每秒加载一次,但它永远不会到达 timer1 滴答事件中的代码,因为它卡在这一行的 While 循环中:
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
我想要的是每秒在网络浏览器中加载一个新内容,并且每秒将来自网络浏览器的新内容保存到硬盘上。
我是怎么做到的,因为它卡在了 While 循环中?