C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

C# Source Code


Welcome to Dream.In.Code
Become a C# Expert!

Join 340,144 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 3,811 people online right now. Registration is fast and FREE... Join Now!




Save a bitmap of a web page

Submitted By: gabehabe
Actions:
Rating:
Views: 5,242

Language: C#

Last Modified: September 25, 2008

Snippet


  1. /*
  2. * Author: Danny Battison
  3. * Contact: gabehabe@googlemail.com
  4. */
  5.  
  6. /// <summary>
  7. /// A method to capture a webpage as a System.Drawing.Bitmap
  8. /// </summary>
  9. /// <param name="URL">The URL of the webpage to capture</param>
  10. /// <returns>A System.Drawing.Bitmap of the entire page</returns>
  11. public System.Drawing.Bitmap CaptureWebPage(string URL)
  12. {
  13.         // create a hidden web browser, which will navigate to the page
  14.         System.Windows.Forms.WebBrowser web = new System.Windows.Forms.WebBrowser();
  15.         web.ScrollBarsEnabled = false; // we don't want scrollbars on our image
  16.         web.ScriptErrorsSuppressed = true; // don't let any errors shine through
  17.         web.Navigate(URL); // let's load up that page!
  18.                        
  19.         // wait until the page is fully loaded
  20.         while (web.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
  21.                 System.Windows.Forms.Application.DoEvents();
  22.         System.Threading.Thread.Sleep(1500); // allow time for page scripts to update
  23.         // the appearance of the page
  24.                        
  25.         // set the size of our web browser to be the same size as the page
  26.         int width = web.Document.Body.ScrollRectangle.Width;
  27.         int height = web.Document.Body.ScrollRectangle.Height;
  28.         web.Width = width;
  29.         web.Height = height;
  30.         // a bitmap that we will draw to
  31.         System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height);
  32.         // draw the web browser to the bitmap
  33.         web.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));
  34.                        
  35.         return bmp; // return the bitmap for processing
  36. }

Copy & Paste


Comments

gbertoli3 2008-09-26 07:58:56

You're on a roll!

Lotharyx 2008-11-24 15:48:19

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).

gabehabe 2008-11-25 06:12:27

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

bongenie 2008-11-29 20:26:39

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

chaitanyavswi 2009-08-01 08:58:32

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month