繁体   English   中英

线程和位图方法C#

[英]threading & bitmap method c#

我正在尝试从c#中的浏览器控件获取屏幕截图。 当我尝试初始化浏览器控件时,出现错误:

由于当前线程不在单线程单元中,因此无法实例化ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2'。

现在,我正在尝试创建一个线程,并在其中进行所有初始化,但是在尝试声明Response.ContentType = "image/jpeg";时出现错误Response.ContentType = "image/jpeg"; 错误消息是:

在这种情况下,响应不可用。

这是我完整的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{

    string currentPageUrl = "";
    Bitmap bmp = new Bitmap(1024, 768);

    protected void Page_Load(object sender, EventArgs e)
    {       
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (Request.ServerVariables["HTTPS"].ToString() == "")
        {
            currentPageUrl = Request.ServerVariables["SERVER_PROTOCOL"].ToString().ToLower().Substring(0, 4).ToString() + "://" + Request.ServerVariables["SERVER_NAME"].ToString() + ":" + Request.ServerVariables["SERVER_PORT"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
        }
        else
        {
            currentPageUrl = Request.ServerVariables["SERVER_PROTOCOL"].ToString().ToLower().Substring(0, 5).ToString() + "://" + Request.ServerVariables["SERVER_NAME"].ToString() + ":" + Request.ServerVariables["SERVER_PORT"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
        }

        Thread thr = new Thread(new ThreadStart(OptimizeWebBrowserAndCreateImage));
        thr.SetApartmentState(ApartmentState.STA);
        thr.Start();   
    }   

    protected void OptimizeWebBrowserAndCreateImage()
    {
        System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
        browser.ScrollBarsEnabled = false;
        browser.ScriptErrorsSuppressed = true;
        browser.Navigate(currentPageUrl);
        while (browser.ReadyState != WebBrowserReadyState.Complete)
            System.Windows.Forms.Application.DoEvents();
        System.Threading.Thread.Sleep(1500);
        int width = browser.Document.Body.ScrollRectangle.Width;
        int height = browser.Document.Body.ScrollRectangle.Height;
        browser.Width = width;
        browser.Height = height;
        System.Drawing.Bitmap bmp = new Bitmap(width, height);
        browser.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));      
        Response.ContentType = "image/jpeg";
        Response.AppendHeader("Content-Disposition", "attachment; filename=screenshot.jpg");
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        bmp.Dispose();
        bmp.Dispose();
        Response.End();
    }
}

正如我在Response.ContentType = "image/jpeg";上所说的那样,我得到了一个错误Response.ContentType = "image/jpeg";

感谢您的帮助,希望有人可以帮助我解决此问题。 在此先感谢Laziale

Updated Version based on Henk's advice:using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

public partial class _Default : System.Web.UI.Page
{

    string currentPageUrl = "";
    Bitmap bmp = new Bitmap(1024, 768);
    Thread thr = null;

    protected void Page_Load(object sender, EventArgs e)
    {       
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        if (Request.ServerVariables["HTTPS"].ToString() == "")
        {
            currentPageUrl = Request.ServerVariables["SERVER_PROTOCOL"].ToString().ToLower().Substring(0, 4).ToString() + "://" + Request.ServerVariables["SERVER_NAME"].ToString() + ":" + Request.ServerVariables["SERVER_PORT"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
        }
        else
        {
            currentPageUrl = Request.ServerVariables["SERVER_PROTOCOL"].ToString().ToLower().Substring(0, 5).ToString() + "://" + Request.ServerVariables["SERVER_NAME"].ToString() + ":" + Request.ServerVariables["SERVER_PORT"].ToString() + Request.ServerVariables["SCRIPT_NAME"].ToString();
        }
        thr = new Thread(new ThreadStart(OptimizeWebBrowserAndCreateImage));
        thr.SetApartmentState(ApartmentState.STA);
        thr.Start();      
    }  

    protected void OptimizeWebBrowserAndCreateImage()
    {
        /*
        Bitmap bitmap = new Bitmap(CaptureWebPage2(currentPageUrl));
        Response.ContentType = "image/jpeg";
        Response.AppendHeader("Content-Disposition", "attachment; filename=screenshot.jpg");
        bitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
        bitmap.Dispose();
        bitmap.Dispose();
        Response.End();
        */

        System.Windows.Forms.WebBrowser browser = new System.Windows.Forms.WebBrowser();
        browser.ScrollBarsEnabled = false;
        browser.ScriptErrorsSuppressed = true;
        browser.Navigate(currentPageUrl);
        while (browser.ReadyState != WebBrowserReadyState.Complete)
            System.Windows.Forms.Application.DoEvents();
        System.Threading.Thread.Sleep(1500);
        int width = browser.Document.Body.ScrollRectangle.Width;
        int height = browser.Document.Body.ScrollRectangle.Height;
        browser.Width = width;
        browser.Height = height;
        System.Drawing.Bitmap bmp = new Bitmap(width, height);
        browser.DrawToBitmap(bmp, new System.Drawing.Rectangle(0, 0, width, height));      
     /*  Response.ContentType = "image/jpeg";
        Response.AppendHeader("Content-Disposition", "attachment; filename=screenshot.jpg");
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
        bmp.Dispose();
        bmp.Dispose();
        Response.End();
      */
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        if (thr != null)
        {
            thr.Join();

            Response.ContentType = "image/jpeg";
            Response.AppendHeader("Content-Disposition", "attachment; filename=screenshot.jpg");
            bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
            bmp.Dispose();
            //bmp.Dispose();
            Response.End();
        }
    }
}

您在浏览器中,因此可以使用WebBrowser的Document属性获取网页的数据。
如何从文档中获取数据,则取决于文档本身。
我也看不到您在哪里启动响应。 发布代码中是否缺少此代码?

您正在运行一个单独的线程,因此该线程正在HttpContext外部运行。 这就是为什么您没有得到有用的响应(或请求或会话)的原因。 因此,将Context作为参数传递给您的方法,并从那里获取响应。

或者重写代码,以使该方法不依赖于HttpContext(因为这可能不是线程安全的)。 在页面生命周期的稍后阶段拾取位图结果,您可以在其中使用响应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM