我正在尝试从c#中获取浏览器控件的屏幕截图。当我尝试初始化浏览器控件时,我收到一个错误:
ActiveX控件'8856f961-340a-11d0-a96b-00c04fd705a2'不能 实例化,因为当前线程不在单线程中 公寓。
现在我正在尝试的是我正在创建一个线程并且我正在进行所有初始化,但是当我尝试声明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";
感谢您的帮助,我希望有人可以帮我解决这个问题。 在此先感谢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();
}
}
}
答案 0 :(得分:0)
您在浏览器中,因此您可以使用WebBrowser的 Document 属性获取网页的数据。
如何从文档中获取数据,取决于文档本身
我也看不到你在哪里开始响应。发布的代码中是否缺少此内容?
答案 1 :(得分:0)
您正在运行一个单独的线程,因此它在HttpContext之外运行。这就是为什么你没有得到有用的响应(或请求或会话)。因此,将Context作为参数传递给您的方法并从那里获取响应。
或者重写代码,以便该方法不依赖于HttpContext(因为它可能不是线程安全的)。稍后在页面生命周期中获取位图结果,您可以在其中获得可以使用的响应。