asp.net - C# WebBrowser 控件将网页另存为图片,重定向问题

标签 asp.net image redirect webbrowser-control

我在控制台应用程序中使用 Web 浏览器控件,它是以编程方式创建的。它通过一个 url 并使用 DrawToBitmap 将页面保存为图像文件。

它似乎适用于许多 URL,但不适用于某些 URL,包括保存空白页面的 www.google.co.uk。 IBM.com 和 microsoft.com 工作,认为这可能与网站进行重定向有关。

这是代码:

 int width = -1;
        int height = -1;

        // Load the webpage into a WebBrowser control
        WebBrowser wb = new WebBrowser();
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        wb.Navigated += new WebBrowserNavigatedEventHandler(wb_Navigated);

        wb.AllowNavigation = true;
        wb.ScrollBarsEnabled = false;
        wb.ScriptErrorsSuppressed = true;
        wb.Navigate(url);

        while (wb.ReadyState != WebBrowserReadyState.Complete)
        {
            Debug.WriteLine("Loading loop..");
            Application.DoEvents();
        }



        // Set the size of the WebBrowser control
        wb.Width = width;
        wb.Height = height;

        if (width == -1)
        {
            // Take Screenshot of the web pages full width
            wb.Width = wb.Document.Body.ScrollRectangle.Width;
        }

        if (height == -1)
        {
            // Take Screenshot of the web pages full height
            wb.Height = wb.Document.Body.ScrollRectangle.Height;
        }

        // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
        Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
        wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
        wb.Dispose();

        bitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png);
        Debug.WriteLine("Saved");
        Application.Exit();

任何想法为什么有些网站不起作用?

谢谢你的时间

最佳答案

你可能是想在它加载完成之前保存,检测WB是否满载是WB控制的最大问题,我多年来开发了几种方法,所以它涉及很多工作,并且有各种各样的方法你将需要实现。

另外,不要忘记,一些 HTML 数据可能正在加载到框架中,因此您可能需要获取对框架的引用并对每个框架和这些框架中的所有框架执行相同的操作(必须检测无限嵌套的嵌套框架)并添加到您的对象引用中以供使用)- 可靠的加载完整检测也需要帧引用,单独检查 readystate 或 .busy 是非常不可靠的,您的代码几乎肯定会在大多数情况下崩溃。查看我的一些与 WB 相关的帖子以获取更多信息,或者:Webbrowser Control Limitations

另外,如果您需要更多帮助,请告诉我,我可以为您提供有关检测加载完成的方法的指示。

关于asp.net - C# WebBrowser 控件将网页另存为图片,重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9121870/

相关文章:

android - 如何在 webview 中捕获网站重定向

regex - WordPress 重定向插件在目标 URL 中包含查询参数

cakephp 2 使用主题标签重定向 url

asp.net - 身份服务器 4 使用有效访问 token 获取 401 未授权

c# - 在SQL Server中存储空值

java - Android最佳实践——下载图片

javascript - Jquery,隐藏img onload

android - 无需谷歌云消息(GCM)将通知从 PC 推送到 android

asp.net - 编写自定义 404 错误页面时如何保留 URL 上的参数?

Java Graphics2D - 绘制具有渐变不透明度的图像