问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/9121870/c-sharp-webbrowser-control-save-web-page-as-image-redirect-issue