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