网站中的网络浏览器类 asp.net

本文关键字:asp net 浏览器 网络 网站 | 更新日期: 2023-09-27 18:34:55

我有一个要求,需要去一个网页并截取它的屏幕截图。

为此,我必须创建一个 ASP.NET,以便用户可以输入网站URL。

我试图创建一个 Web 浏览器控件......但后来意识到我无法在 asp.net 网站中创建 Web 浏览器控件。

我正在尝试做这样的事情 http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx 有人可以帮助我如何开始,因为我知道我们只能在 Windows 窗体中使用 Web 浏览器组件,而不是在网站上。

不确定我的问题是否太愚蠢,我错过了一些东西......有人可以帮助我吗

PS :如果我的问题不清楚,请告诉我。我会尽量更具体!

网站中的网络浏览器类 asp.net

我们需要添加带有线程的Web浏览器,否则我们将得到无法实例化 ActiveX 控件"8856f961-340a-11d0-a96b-00c04fd705a2",因为当前线程不在单线程单元中。错误

这是我们可以使网络浏览器在 asp.net 网页中工作的方式

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;
        }
    }
}

在网页中

CustomBrowser browser = new CustomBrowser();
string s = browser.GetWebpage("http://localhost:8781/WebSite3/Default3.aspx");
Response.Write(s);

您可以在 ASP.net 中使用 WebBrowser 控件服务器端。只需添加对 System.Windows.Forms 的引用。

您可能可以使用 Web 处理程序文件 (.ashx( 而不是 .aspx。这将允许您在没有 Web 窗体的情况下使用 Web 浏览器控件。