【问题标题】:Unable waiting for web page loading using WebBrowser control无法使用 WebBrowser 控件等待网页加载
【发布时间】:2019-01-28 10:58:30
【问题描述】:

我正在编写一项服务,其中包含本地网站网页的屏幕截图。 我正在使用 WebBrowser 控件将网页捕获为图像。 以下是我正在使用的代码:

namespace MakeScreenShotURL
{
    class Program
    {
        private static Config config = new Config(System.Reflection.Assembly.GetExecutingAssembly().Location);
        [STAThread]
        static void Main()
        {
            int width = 1700;
            int height = 1700;
            string username = "userdata";
            string password = "passworddata";
            int currentmonth = DateTime.Now.Month;
            int nextmonth = DateTime.Now.Month + 1;
            int year = DateTime.Now.Year;
            screen(width, height, year, nextmonth, currentmonth, username, password);
        }

        static void screen(int width, int height, int year, int nextmonth, int currentmonth, string username, string password)
        {
            string PlaceCode = config.GetAppSetting("place");
            string[] Places = PlaceCode.Split(';'); //contains a list of sites for example: Berlin; Munich; Dresden
            foreach (string pl in Places)  
            {
                using (WebBrowser browser = new WebBrowser())
                {
                    browser.Width = width;
                    browser.Height = height;
                    browser.ScrollBarsEnabled = true;
                    Uri uri = new Uri(" http://localsite.php?y=" + year + "&m=" + nextmonth + "&p=" + pl + " ");
                    string additionalHeaderInfo = "Authorization: Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(username + ":" + password)) + System.Environment.NewLine;
                    browser.Navigate(uri, null, null, additionalHeaderInfo);
                    Wait(2);  //waiting for pages to load 2 seconds
                    using (Graphics graphics = browser.CreateGraphics())
                    using (Bitmap bitmap = new Bitmap(browser.Width, browser.Height, graphics))
                    {
                        Rectangle bounds = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
                        browser.DrawToBitmap(bitmap, bounds);
                        bitmap.Save("C:/Users/user/image/screenshot" + pl + ".jpeg", ImageFormat.Jpeg); 
                    }
                    Application.Run();
                }
            }
            Application.Exit();
        }
        static void Wait(int number)
        {
            DateTime time = DateTime.Now;
            do
            {
                Application.DoEvents();
            }
            while (time.AddSeconds(number) > DateTime.Now);
        }
    }
}

程序从 Places 数组中创建第一个元素(柏林)的屏幕截图并将其保存到磁盘。 但是当从数组中选择第二个元素时,程序在该行之后挂起执行:

Application.Run();

也许程序需要一些东西,但我无法理解。

有人可以帮我吗?

【问题讨论】:

    标签: c# .net winforms webbrowser-control


    【解决方案1】:

    我真的很感兴趣,因为它没有父窗口,也没有可供 WebBrowser 交互的用户界面。

    无论如何,Application.Run() 进入主消息循环,因此它等待消息。这个循环通常会一直运行直到被强制停止,例如通过发送 WM_QUIT 消息。

    Application.DoEvents() 做了类似的事情,但它不会阻塞。它只是查看消息队列内部,执行其中的所有消息,然后返回。因此,您可以删除对 Application.Run() 的调用。

    如果您正在创建一个严肃的应用程序,您至少需要开始使用事件。将方法附加到 WebBrowser.DocumentCompleted 并在那里等待页面加载,然后运行对 browser.DrawToBitmap 的调用。

    您还需要提供比 DoEvents 更好的消息执行方式,因为您当前的解决方案基本上会在浏览器加载时占用一个 CPU 内核。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 2012-12-01
      • 2013-11-05
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 2018-08-13
      • 2013-11-24
      相关资源
      最近更新 更多