2011-08-01 19 views
1

我想從C#中的瀏覽器控件獲取屏幕截圖。當我試圖初始化瀏覽器控件,我得到一個錯誤:線程和位圖方法c#

ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.

現在我想做的是,我創建一個線程,我所做的一切initizalization那裏,但我得到一個錯誤當我試圖聲明Response.ContentType = "image/jpeg"; 的錯誤信息是:

Response is not available in this context.

這裏是我的完整代碼:

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

嘿亞歷克斯,感謝您的評論。然而,線程似乎很好,問題在於響應。當我不使用任何線程時,響應沒有任何問題。 – Laziale

+0

@Russ C如果你的意思是我的page_load事件,不,它沒有裝飾。它雖然是asp.net(c#)頁面。感謝您的幫助 – Laziale

+0

好點,好工作,但它是一個即興的:D –

回答

0

您正在運行一個單獨的線程,以便在HttpContext之外運行。這就是爲什麼你沒有得到有用的迴應(或請求或會話)。因此,將Context作爲參數傳遞給您的方法,並從那裏獲取響應。

或重寫您的代碼,以便該方法不依賴於HttpContext(因爲這可能不是線程安全的)。稍後在頁面生命週期中獲取位圖結果,您可以使用該響應。

+0

我不認爲這將是線程安全的。 –

0

你是在瀏覽器中,因此,你可以使用WebBrowser的文檔屬性來獲取網頁的數據。
如何從文檔中獲取數據,取決於文檔本身。
我也無法看到你在哪裏使這個反應活躍起來。這在發佈的代碼中是否丟失?