2

I have a requirement which needs to go to a webpage and take a screenshot of it.

for this I have to create a ASP.NET so that the user can enter the website URL..

I was trying to create a web browser control.. but later realized I cant create web browser control in a asp.net website ..

I am trying to do something like this http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx can some one help me how to start of with this as i know we can only use web browser component in windows forms and not on website..

I am not sure if my question is too silly and i am missing something.. can some one help me

PS : Let me know if my question is unclear.. I ll try to be more specific!

helpme
  • 570
  • 1
  • 7
  • 26
  • See: http://stackoverflow.com/questions/2316564/taking-screenshot-of-a-webpage-programmatically – Zachary May 21 '12 at 15:41
  • Theoretically, couldn't you simply include the control from the relevant DLL and execute it server side? Even though you're not running a client app, you should still be able to include all the DLLs and execute the code. – Tejs May 21 '12 at 15:43

3 Answers3

13

we need to add webbrowser with threading other wise we will get ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment. error

this is the way we can make webbrowser work in asp.net webpages

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Windows.Forms;

/// <summary>
/// Summary description for CustomBrowser
/// </summary>
public class CustomBrowser
{
    public CustomBrowser()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    protected string _url;
    string html = "";
    public string GetWebpage(string url)
    {
        _url = url;
        // WebBrowser is an ActiveX control that must be run in a
        // single-threaded apartment so create a thread to create the
        // control and generate the thumbnail
        Thread thread = new Thread(new ThreadStart(GetWebPageWorker));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        string s = html;
        return s;
    }

    protected void GetWebPageWorker()
    {
        using (WebBrowser browser = new WebBrowser())
        {
            //  browser.ClientSize = new Size(_width, _height);
            browser.ScrollBarsEnabled = false;
            browser.ScriptErrorsSuppressed = true;
            browser.Navigate(_url);

            // Wait for control to load page
            while (browser.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();

            html = browser.DocumentText;

        }
    }

}

in webpage

CustomBrowser browser = new CustomBrowser();
string s = browser.GetWebpage("http://localhost:8781/WebSite3/Default3.aspx");
Response.Write(s);
kavali rakesh
  • 198
  • 1
  • 7
4

You can use the WebBrowser control server-side in ASP.net. Just add a reference to System.Windows.Forms.

Chris Dworetzky
  • 940
  • 4
  • 9
  • What did you do? Any time I do it, I get an Active X threading error, and when fixing that, it "runs" but the Print() does nothing at all. (No exceptions either.) – Katastic Voyage Dec 19 '16 at 19:24
0

You can probably use a web handler file (.ashx) instead of .aspx. This will allow you to use a webbrowser control without a webform.

user385599
  • 25
  • 1
  • 8