穿线及;位图方法c#
我正在尝试从c#中的浏览器控件获取屏幕截图。尝试初始化浏览器控件时,出现错误: ActiveX控件“8856f961-340a-11d0-a96b-00c04fd705a2”不能为空 实例化,因为当前线程不在单线程中 公寓 现在我尝试的是创建一个线程,并在那里进行所有初始化,但是当我试图声明穿线及;位图方法c#,c#,asp.net,winforms,multithreading,C#,Asp.net,Winforms,Multithreading,我正在尝试从c#中的浏览器控件获取屏幕截图。尝试初始化浏览器控件时,出现错误: ActiveX控件“8856f961-340a-11d0-a96b-00c04fd705a2”不能为空 实例化,因为当前线程不在单线程中 公寓 现在我尝试的是创建一个线程,并在那里进行所有初始化,但是当我试图声明Response.ContentType=“image/jpeg”时,我遇到了一个错误 错误消息是: 响应在此上下文中不可用 这是我的完整代码: using System; using System.Coll
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();
}
}
}
您在浏览器中,因此可以使用WebBrowser的Document属性获取网页的数据
如何从文档中获取数据取决于文档本身。
我也看不出你是从哪里开始回复的。发布的代码中是否缺少该线程?您正在运行一个单独的线程,因此该线程在HttpContext之外运行。这就是为什么您没有得到有用的响应(或请求或会话)。因此,将上下文作为参数传递给您的方法并从中获得响应
或者重写代码,使方法不依赖于HttpContext(因为这可能不是线程安全的)。稍后在页面生命周期中选择位图结果,您可以在其中使用响应。嘿,Alex,谢谢您的评论。然而,线程似乎很好,问题在于响应。当我不使用任何线程时,响应出来时没有任何问题。@Russ C如果你指的是我的页面加载事件,不,它没有被修饰。不过这是asp.net(c#)页面。谢谢你的帮助好的观点,好的工作这是一个即兴的但是:D