4

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.

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.

This is the 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?

Thanks for your time

JeremyBeadle
  • 683
  • 1
  • 8
  • 23
  • www.google.co.uk doesn't redirect for everyone. Where are your testing servers/workstations running, which locale? Have you tested it with other server, including localhost URLs that do immediate HTTP Location redirects? – Jon Adams Feb 03 '12 at 00:09
  • In England so it should be .co.uk, .com doesn't work either. Will try and test it on another server. It may not be redirects but its something that only seems to affect certain sites – JeremyBeadle Feb 03 '12 at 08:32

1 Answers1

0

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.

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.

Community
  • 1
  • 1
Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92