线程,位图方法c#
本文关键字:方法 位图 线程 | 更新日期: 2023-09-27 18:04:02
我试图在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";
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();
}
}
}
您正在运行一个单独的线程,因此它在HttpContext之外运行。这就是为什么您没有得到有用的响应(或请求或会话)。所以将Context作为参数传递给你的方法并从那里获得Response。
或者重写你的代码,使方法不依赖于HttpContext(因为这可能不是线程安全的)。你在浏览器内部,因此你可以使用WebBrowser的Document属性获取网页的数据。
如何从文档中获取数据,取决于文档本身。
我也看不出你在哪里初始化回复。张贴的代码中缺少这个吗?