• How to capture screen shot of a specific URL in C#

    by  • April 23, 2014 • Microsoft .NET C# (C-Sharp), Microsoft ASP.NET • 0 Comments

    In my previous article, I have shared code snippet about capturing current webpage screen shot. In following article, I will show you the code snippet of capturing screen shot of a given URL. This URL may be your website or any other website. For this purpose, you just need to use the WebBrowser control of Windows Forms, open that URL in WebBrowser control and get the image of that website. Then you can display it or you can use it for your purpose. So the idea is to get the bitmap image of a particular webpage using WebBrowser control.

    First we need to add reference of Windows.Forms

    Add reference

    In following code snippet, I have created CaptureScreenShot() method with three parameters named url, width and height. In method body, I have initialized a new thread. Then inside the thread, I have created a new WebBrowser control and set properties of the control. We need to start a new thread for WebBrowser control and execute the thread in single threaded apartments because ASP.NET applications work in multithreaded apartments. I have called the Navigate() method to browse the URL. I have attached DocumentCompleted event handler with WebBrowser control which will be raised when web page is loaded in the WebBrowser control. In DecumentCompleted event handler body, I have called DrawToBitmap() method of WebBrowser control to get the bitmap image of the webpage. At the end, I have converted image to Base64 string by using byte array.

    Namespaces

    Write following method 

    Now write following WebBrowser event handler

    At the end, just call the method by providing parameters.

    You can download entire project code sample from below link.

     




    Namecheap.com
    Avatar of GetCodeSnippet.com

    About

    GetCodeSnippet.com provides you high quality Microsoft .NET development related articles, tutorials and sample code for ASP.NET, C#, VB.NET, AJAX, WCF, WPF, Web Services, Windows Services, SQL Server and other technologies.

    Leave a Reply