Capture Screenshot of Website from URL in C#

Updated on     Kisan Patel

Problem:

How to Capture Screenshot of Website from URL in C#?
Programmatically get a screenshot of a page in C#
How do take a screenshot of a webpage programmatically given the URL as input?

Solution:

If you want to capture a screenshot of a complete website, take a look at the following snippet.

private void WebsiteScreenshot(string url, string file)
{
    //Create a webbrwoser object
    WebBrowser browser = new WebBrowser();
 
    //Deactivate scrollbars, unless you want them to
    //appear on your screenshot
    browser.ScrollBarsEnabled = false;
 
    //Suppress (java-)script error popups
    browser.ScriptErrorsSuppressed = true;
 
    //Open the given url in webbrowser
    browser.Navigate(new Uri(url));
 
    //Wait until the page is fully loaded
    while (browser.Document == null || browser.Document.Body == null)
    Application.DoEvents();
 
    //Resize the webbrowser object to the same size as the
    //webpage
    Rectangle websiteSize = browser.Document.Body.ScrollRectangle;
    browser.Size = new Size(websiteSize.Width,websiteSize.Height);
 
    //Create a bitmap object with the same dimensions as the website
    Bitmap bmp = new Bitmap(websiteSize.Width, websiteSize.Height);
 
    //Paint the website contents to the bitmap
    browser.DrawToBitmap(bmp,
    new Rectangle(0, 0, websiteSize.Width, websiteSize.Height));
 
    browser.Dispose();
 
    //Save the bitmap at the given filepath
    bmp.Save(file, ImageFormat.Png);
}

You can then call the method as follows:

//If you want file extension than .png, don't forget to change the
//ImageFormat-type in the above main function!
WebsiteScreenshot("http://csharpcode.org/", "c:\website_screenshot.png");

C#

Leave a Reply