Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi ,

when i am using below code request is running but not getting the response.. when i am using the STA threads

how to give the permeation to allow the STA threads in IIS

C#
public partial class Test : Page
{
    bool snap = false;
    protected void CreateThumbnailImage(object sender, EventArgs e)
    {            
        snap = true;
    }
    protected override void Render(HtmlTextWriter writer)
    {
        if (snap)
        {
            int width = Int32.Parse(txtWidth.Text);
            int height = Int32.Parse(txtHeight.Text);
            StringBuilder builder = new StringBuilder();
            HtmlTextWriter htw = new HtmlTextWriter(new StringWriter(builder));
            base.Render(htw);
            string html = builder.ToString();
            Thumbnail thumbnail = new Thumbnail(html, 800, 600, width, height, Thumbnail.ThumbnailMethod.Html);
            Bitmap image = thumbnail.GenerateThumbnail();
            image.Save(Server.MapPath("~") + "/Thumbnail.bmp");                
            writer.Write(html);
            writer.Write("<img src=\"Thumbnail.bmp\" />");
        }
        else
            base.Render(writer);
    }
}
public class Thumbnail
{
    public enum ThumbnailMethod { Url, Html };
    public string Url { get; set; }
    public Bitmap ThumbnailImage { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public int BrowserWidth { get; set; }
    public int BrowserHeight { get; set; }
    public string Html { get; set; }
    public ThumbnailMethod Method { get; set; }
 
    public Thumbnail(string data, int browserWidth, int browserHeight, int thumbnailWidth, 
                                    int thumbnailHeight, ThumbnailMethod method)
    {
        this.Method = method;
        if (method == ThumbnailMethod.Url)
            this.Url = data;
        else if (method == ThumbnailMethod.Html)
            this.Html = data;
        this.BrowserWidth = browserWidth;
        this.BrowserHeight = browserHeight;
        this.Height = thumbnailHeight;
        this.Width = thumbnailWidth;
    }
    public Bitmap GenerateThumbnail()
    {
        Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();
        return ThumbnailImage;
    }
    private void GenerateThumbnailInteral()
    {
        WebBrowser webBrowser = new WebBrowser();
        webBrowser.ScrollBarsEnabled = false;
        if (this.Method == ThumbnailMethod.Url)
            webBrowser.Navigate(this.Url);
        else webBrowser.DocumentText = this.Html;
        webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
        while (webBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
        webBrowser.Dispose();
    }
    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        WebBrowser webBrowser = (WebBrowser)sender;
        webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
        webBrowser.ScrollBarsEnabled = false;
        this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
        webBrowser.BringToFront();
        webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
        this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
    }
}
Posted
Comments
Philippe Mori 22-Nov-14 11:04am    
First of all if you are running an ASP.NET application, I don't think that using a WebBrowser is an appropriate solution as such application does not have an UI.
Then even then, I don't think that your code make much sense. Either the browser is displayed by the main thread which is STA as far as I know, or you don't use the control if you don't display it.
In my opinion, the whole idea is wrong to start with. By the way, you should explain what you are trying to do as it might help us to point you in the right direction.

1 solution

You can NOT use a WebBrowser control in an ASP.NET app.

First, there is no point to it because all of the code in an ASP.NET app runs entirely on the SERVER! The users of your ASP.NET application will never see your web browser control.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900