6 Replies - 2181 Views - Last Post: 26 September 2011 - 04:26 AM

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Save a bitmap of a web page

Posted 24 September 2008 - 09:48 AM

Description:
/*
 * Author: Danny Battison
 * Contact: [email protected]
 */

/// 
/// A method to capture a webpage as a System.Drawing.Bitmap
/// 
/// The URL of the webpage to capture
/// A System.Drawing.Bitmap of the entire page
public System.Drawing.Bitmap CaptureWebPage(string URL)
{
	System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
	web.ScrollBarsEnabled = false;
	web.ScriptErrorsSuppressed = true;
	web.Navigate(URL);
			
	// wait until the page is fully loaded
	while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
		System.Windows.Forms.Application.DoEvents();
	System.Threading.Thread.Sleep(1500);
			
	int width = web.document.Body.ScrollRectangle.Width;
	int height = web.document.Body.ScrollRectangle.Height;
	web.Width = width;
	web.Height = height;
	System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
	web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
			
	return bmp;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Save a bitmap of a web page

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: Save a bitmap of a web page

Posted 24 September 2008 - 09:48 AM

Description:
/*
 * Author: Danny Battison
 * Contact: [email protected]
 */

/// 
/// A method to capture a webpage as a System.Drawing.Bitmap
/// 
/// The URL of the webpage to capture
/// A System.Drawing.Bitmap of the entire page
public System.Drawing.Bitmap CaptureWebPage(string URL)
{
	// create a hidden web browser, which will navigate to the page
	System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
	web.ScrollBarsEnabled = false; // we don't want scrollbars on our image
	web.ScriptErrorsSuppressed = true; // don't let any errors shine through
	web.Navigate(URL); // let's load up that page!
			
	// wait until the page is fully loaded
	while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
		System.Windows.Forms.Application.DoEvents();
	System.Threading.Thread.Sleep(1500); // allow time for page scripts to update
	// the appearance of the page
			
	// set the size of our web browser to be the same size as the page
	int width = web.document.Body.ScrollRectangle.Width;
	int height = web.document.Body.ScrollRectangle.Height;
	web.Width = width;
	web.Height = height;
        // a bitmap that we will draw to
	System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
	// draw the web browser to the bitmap
	web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
			
	return bmp; // return the bitmap for processing
}

Was This Post Helpful? 0
  • +
  • -

#3 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 41
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: Save a bitmap of a web page

Posted 26 September 2008 - 04:58 AM

You're on a roll!
Was This Post Helpful? 0
  • +
  • -

#4 Lotharyx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 24-November 08

Re: Save a bitmap of a web page

Posted 24 November 2008 - 01:48 PM

This would be great if it actually worked. Unfortunately, it doesn't. From Microsoft .NET documentation of the System.Windows.Forms.WebBrowser class: DrawToBitmap - Infrastructure. This method is not supported by this control. (Inherited from WebBrowserBase.) As documented, the DrawToBitmap method of the WebBrowser object is not implemented. Try this code and you'll get a blank bitmap (yes, this is experience speaking).
Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1430
  • View blog
  • Posts: 11,006
  • Joined: 06-February 08

Re: Save a bitmap of a web page

Posted 25 November 2008 - 04:12 AM

Works for me, I've used it a bunch of times. The only problem can be when scripts need to be run, it might not allow time for all of them to load. Trust me on this one, I test my snippets a bunch of times before submitting them. ;)
Was This Post Helpful? 0
  • +
  • -

#6 bongenie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 29-November 08

Re: Save a bitmap of a web page

Posted 29 November 2008 - 06:26 PM

I don't have a C# compiler and I don't know how to use one... But I'm very interested in this code. Is there any possibility to have this function in a dll file to use it from an other language ? Thanks to everyone could help me Daniel
Was This Post Helpful? 0
  • +
  • -

#7 chaitanyavswi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 01-August 09

Re: Save a bitmap of a web page

Posted 01 August 2009 - 05:58 AM

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
Was This Post Helpful? 0
  • +
  • -

#8 Johan1974  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-April 09

Re: Save a bitmap of a web page

Posted 26 September 2011 - 04:26 AM

Nice code. I get a bmp only the size is small when i have for example only three lines of text in the html. Can i force the size of the image to be A4?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1