C#WebBrowser控件保存网页为图片

C#WebBrowser控件保存网页为图片

本文介绍了C#WebBrowser控件保存网页为图片,重新导向问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用一个控制台应用程序的Web浏览器控件,它是一个编程创建。它传递了一个网址,并使用DrawToBitmap保存页面为图像文件。

I am using the web browser control in a console application, it is created programatically. It gets passed a url and saves the page as an image file using DrawToBitmap.

这似乎对很多网址,而不是有些人,包括www.google.co.uk它节省了一个空白页正常工作。 IBM.com和microsoft.com的工作,认为它可能有一些做的网站做一个重定向。

It seems to work fine for many URLs but not for some, including www.google.co.uk where it saves a blank page. IBM.com and microsoft.com work, think it might have something to do with the site doing a redirect.

这是code:

 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();

任何想法,为什么某些网站不工作?

Any ideas why some sites don't work?

感谢您的时间

推荐答案

您可能正在试图挽救它,它加载完成之前,检测是否WB满载是WB控制的最大的问题,我已经开发了几种方法多年来,所以它涉及到大量的工作,并且有你需要实现各种方法。

You are probably trying to save it before it has completed loading, detecting if WB is fully loaded is the biggest problem of the WB control, i have developed several methods over the years, so it involved a lot of work, and there are various methods you will need to implement.

另外,不要忘了,一些HTML数据可能被加载在一个框架,所以你可能需要去德框架的引用,做同样的事情的每一帧,并在这些帧的所有帧(无限嵌套嵌套框架必须检测并添加到您的对象引用使用) - 也需要可靠的加载完整的检测帧参考,检查的readyState或.busy单纯是出奇的不可靠,你的code将突破几乎可以肯定的大部分时间。检查出我的一些WB相关职位更多,或这样:Webbrowser控制限制

Also, don't forget, some HTML data may be loading in a frame, so you may need to get a reference to teh frame and do the same thing for each frame and all frames in those frames (nested frames infinitely nested must be detected and added to your object reference to use) - frame referencing is also needed for reliable loading complete detection, checking readystate or .busy alone is insanely unreliable, your code will break almost certainly most of the time. Check out some of my WB related posts for more, or this: Webbrowser Control Limitations

另外,如果你想要更多的帮助,让我知道,我可以给你检测加载完整方法指针。

Also, if you want more help, let me know, I can give you pointers on methods of detecting loading complete.

这篇关于C#WebBrowser控件保存网页为图片,重新导向问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:06