Is there any alternative to Application.DoEvents()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skavinkar
    New Member
    • Jun 2012
    • 23

    Is there any alternative to Application.DoEvents()

    I have a web application where I am trying to save webpage as image using the code provided at http://stackoverflow.com/questions/2...e-from-asp-net. Somehow I am not able to make it work on my server whereas its working perfectly on my local machine.Not sure about it but I think the line Application.DoE vents() is creating trouble for me , not even sure what it does but I don't get expected result if i remove it.
    Is there any other alternative to Application.DoE vents() or any modification in the code which will solve my problem.
  • Aimee Bailey
    Recognized Expert New Member
    • Apr 2010
    • 197

    #2
    This question was moved because it is ASP.NET specific.

    To add my two cents, while I was writing this reply I had a look at the code you pointed too. The example that Cat Man Do has provided in theory could work, but there are many pitfalls with it, and it's fairly long winded. For example he creates a new thread, yet syncs it with the original, in theory making DoEvents useless in my book. For a bit of fun i've put a little example together myself, although I should mention that i've tested this only on C# WinForms not ASP.NET so it may need a bit of adaptation;

    Code:
    namespace WebBrowserCapture
    {
        using System;
        using System.Drawing;
        using System.Drawing.Imaging;
        using System.Linq;
        using System.Windows.Forms;
        using System.Collections.Generic;
    
        public class WebsiteToImage
        {
            private WebBrowser Browser;
    
            public string URL { get; private set; }
            public List<Bitmap> Output { get; private set; }
            public event EventHandler Complete;
    
            public WebsiteToImage(string url)
            {
                URL = url;
                Output = new List<Bitmap>();
                Browser = new WebBrowser();
                Browser.ClientSizeChanged += delegate
                {
                    Output.Clear();
                };
    
                Browser.DocumentCompleted += delegate
                {
                    var r = Browser.Document.Body.ScrollRectangle;
                    var bmp = new Bitmap(r.Width, r.Height);
                    
                    this.Browser.ClientSize = new Size(r.Width, r.Height);
                    this.Browser.ScrollBarsEnabled = false;
                    this.Browser.BringToFront();
                    this.Browser.DrawToBitmap(bmp, Browser.Bounds);
                    Output.Add(bmp);
    
                    if (Complete != null) Complete(this, null);
                };
            }
    
            public void Generate()
            {
                Output.Clear();
                if (Browser.ReadyState == WebBrowserReadyState.Loading) return;
                Browser.Navigate(this.URL);
            }
    
            public void SaveAsJpeg(int outputIndex, string filename, long quality = 100)
            {
                var parameters = new EncoderParameters(1);
                var codecs = ImageCodecInfo.GetImageDecoders();
                var encoder = codecs.First(t => t.FormatID == ImageFormat.Jpeg.Guid);
                parameters.Param[0] = new EncoderParameter(Encoder.Quality, quality);
                Output[outputIndex].Save(filename, encoder, parameters);
            }
        }
    }
    You may notice I have added a list for the Output, I found that the WebBrowser calls DocumentComplet e a few times, so having a list allows you to see what was actually captured. To use this class, the following code can be written:

    Code:
    WebsiteToImage t = new WebsiteToImage("some-web-address");
    t.Complete += delegate
    {
        t.SaveAsJpeg(0, "some-filename");
    };
    t.Generate();
    Hope this helps in some way.

    Aimee
    Last edited by Aimee Bailey; Jul 31 '12, 07:54 PM.

    Comment

    Working...