code_junkie

Are there any free ways to turn an html page into an image with .net

Nov 11th, 2011
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. public Bitmap GenerateScreenshot(string url)
  2. {
  3. // This method gets a screenshot of the webpage
  4. // rendered at its full size (height and width)
  5. return GenerateScreenshot(url, -1, -1);
  6. }
  7.  
  8. public Bitmap GenerateScreenshot(string url, int width, int height)
  9. {
  10. // Load the webpage into a WebBrowser control
  11. WebBrowser wb = new WebBrowser();
  12. wb.ScrollBarsEnabled = false;
  13. wb.ScriptErrorsSuppressed = true;
  14. wb.Navigate(url);
  15. while (wb.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); }
  16.  
  17.  
  18. // Set the size of the WebBrowser control
  19. wb.Width = width;
  20. wb.Height = height;
  21.  
  22. if (width == -1)
  23. {
  24. // Take Screenshot of the web pages full width
  25. wb.Width = wb.Document.Body.ScrollRectangle.Width;
  26. }
  27.  
  28. if (height == -1)
  29. {
  30. // Take Screenshot of the web pages full height
  31. wb.Height = wb.Document.Body.ScrollRectangle.Height;
  32. }
  33.  
  34. // Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
  35. Bitmap bitmap = new Bitmap(wb.Width, wb.Height);
  36. wb.DrawToBitmap(bitmap, new Rectangle(0, 0, wb.Width, wb.Height));
  37. wb.Dispose();
  38.  
  39. return bitmap;
  40. }
  41.  
  42. // Generate thumbnail of a webpage at 1024x768 resolution
  43. Bitmap thumbnail = GenerateScreenshot("http://pietschsoft.com", 1024, 768);
  44.  
  45. // Generate thumbnail of a webpage at the webpage's full size (height and width)
  46. thumbnail = GenerateScreenshot("http://pietschsoft.com");
  47.  
  48. // Display Thumbnail in PictureBox control
  49. pictureBox1.Image = thumbnail;
  50.  
  51. /*
  52. // Save Thumbnail to a File
  53. thumbnail.Save("thumbnail.png", System.Drawing.Imaging.ImageFormat.Png);
  54. */
Add Comment
Please, Sign In to add comment