如何在WebBrowser页面url内容的每一秒保存图像?

时间:2012-12-21 05:52:40

标签: c#

我正在使用此代码:

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 tick事件中的代码,因为它停留在这一行的While循环中:

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

我想要的是每秒加载一个新的内容或者每秒从web浏览器中保存新的内容。

我是如何做到的,因为它正在陷入While循环?

1 个答案:

答案 0 :(得分:0)