Making WebPage Screenshot | .Net

Making WebPage Screenshot | .Net

Added by Nasir Mahmood updated on Friday, November 17, 2017

Problem:

Many times we want to capture web page as picture. One possible solution is using print screen while viewing page, problem with this solution we can not capture whole page.

This is a small snippet which will capture whole page as a picture and save it to our desired path on disk.

Snippet:

Just copy and paste following code where you want to use.

private void WebPageSnapshot(string url, string outfile)
{
    using (WebBrowser browser = new WebBrowser())
    {
        browser.ScrollBarsEnabled = false;
        browser.Size = Screen.PrimaryScreen.Bounds.Size;
        browser.ScriptErrorsSuppressed = true;                
        browser.Navigate(new Uri(url));
        while (browser.Document == null || browser.Document.Body == null)
        {
          Application.DoEvents();
        }
        Rectangle pageRect = browser.Document.Body.ScrollRectangle;
        browser.Height = pageRect.Height;
        using (Bitmap bmp = new Bitmap(pageRect.Width, pageRect.Height))
        {
          browser.DrawToBitmap(bmp, new Rectangle(0, 0, pageRect.Width, pageRect.Height));
          bmp.Save(outfile, ImageFormat.Png);
        }
    }
}				
				

Usage:

Just call above function with required parameters, where url is web page path and outfile is picture path.

WebPageSnapshot("http://codeplussoft.com", "c:\\snapshot.png");				
				

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected