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

I am using third party open source PAB.WebControls control. It is a wrapper library which allows one to navigate a URL and capture a screen shot of the URL.

I have a strange problem where if I make two sub-sequent calls to one of the method (i.e GetSiteThumbnail), the second request takes close to minute to finish. If I make one request the process finishes in timely fashen (i.e 2 to 3 seconds which is fine). I tried to search and found out that WebBrowser control of .net framework might have memory leak problem. I am not sure whether that is the issue. If someone came across a similar problem and have suggestion, please let me know.

Here is the complete source code:
C#
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Reflection;

namespace PAB.WebControls
{
    public class WebBrowserEx : WebBrowser
    {
        public WebBrowserEx()
        {
        }
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case 0x021:
                case 0x201:
                case 0x204:
                case 0x207:
                    base.DefWndProc(ref m);
                    return;
            }
            base.WndProc(ref m);
        }
    }
    public class WebSiteThumbnail:SWBaseClass
    {
        private string url = null;      
        private Bitmap bmp = null;
        public Bitmap Image 
        {
            get 
            { 
                return bmp; 
            } 
        }
        private ManualResetEvent mre = new ManualResetEvent(false);
        private int timeout = 5; // this could be adjusted up or down
        private int thumbWidth;
        private int thumbHeight;
        private int width;
        private int height;
        private string absolutePath;
        private string fullfilePath;
        #region Static Methods
       
        public static Bitmap GetSiteThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight, string absolutePath, string fullFilePath)
        {
            WebSiteThumbnail thumb = new WebSiteThumbnail(url, width, height, thumbWidth, thumbHeight, absolutePath, fullFilePath);
            Bitmap b = thumb.GetScreenShot();
            if (b == null)
                b = (Bitmap)System.Drawing.Image.FromStream(Assembly.GetExecutingAssembly().GetManifestResourceStream("PAB.WebControls.Notavailable.jpg"));
             return b;
        }
        #endregion
        #region Ctors
       
        public WebSiteThumbnail(string url, int width, int height, int thumbWidth, int thumbHeight,string absolutePath, string fullFilePath)
        {
            this.url = url;
            this.width = width;
            this.height = height;
            this.thumbHeight = thumbHeight;
            this.thumbWidth = thumbWidth;
            this.absolutePath = absolutePath;
            this.fullfilePath = fullFilePath;
        }
        #endregion
        #region ScreenShot
        public Bitmap GetScreenShot()//original keep this
        {
            try
            {
                log.Debug("GetScreenShot begins: ");
                //if (fullfilePath != null && File.Exists(fullfilePath))
                //{
                //    bmp = (Bitmap)System.Drawing.Image.FromFile(fullfilePath);
                //}
                //else
                //{
                    Thread t = new Thread(new ThreadStart(_GetScreenShot));
                    t.SetApartmentState(ApartmentState.STA);
                    t.Start();
                    mre.WaitOne();
                    t.Abort();
                //}
                    log.Debug("GetScreenShot ends: ");
            }
            catch (Exception ex)
            {
                log.Error("Error occured in GetScreenShot: " + ex);
                //throw;
            }
            return bmp;
        }
        
        
        #endregion
        private void _GetScreenShot() 
        {
            try
            {
                log.Debug("_GetScreenShot begins: ");
                WebBrowserEx webBrowser = new WebBrowserEx();
                webBrowser.ScriptErrorsSuppressed = true;
                webBrowser.ScrollBarsEnabled = false;
                DateTime time = DateTime.Now;
                webBrowser.Navigate(url, false); //mahesh added false on 11/16/2010 12:27pm
                webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                while (true)
                {
                    Thread.Sleep(0);
                    TimeSpan elapsedTime = DateTime.Now - time;
                    if (elapsedTime.Seconds >= timeout)
                    {
                        mre.Set();
                    }
                    Application.DoEvents();
                }
                log.Debug("_GetScreenShot ends: ");
            }
            catch (Exception ex)
            {
                log.Error("Error occured in _GetScreenShot: " + ex.InnerException, ex);
            }
        }

        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
        {
            try
            {
                log.Debug("WebBrowser_DocumentCompleted begins: ");
                WebBrowserEx webBrowser = (WebBrowserEx)sender;
                webBrowser.ScriptErrorsSuppressed = true;
                webBrowser.ClientSize = new Size(this.width, this.height);
                webBrowser.ScrollBarsEnabled = false;
                bmp = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
                webBrowser.BringToFront();
                webBrowser.DrawToBitmap(bmp, webBrowser.Bounds);
                Image img = bmp.GetThumbnailImage(thumbWidth, thumbHeight, null, IntPtr.Zero);
                //if (fullfilePath != null && !File.Exists(fullfilePath)) //original 3/8/11
                //{
                //    img.Save(fullfilePath);
                //}
                img.Save(fullfilePath); //new on 3/1/11
                
                bmp = (Bitmap)img;
                webBrowser.Dispose();
                if (mre != null)
                    mre.Set();
                log.Debug("WebBrowser_DocumentCompleted ends: ");
            }
            catch (Exception ex)
            {
                log.Error("Error occured in WebBrowser_DocumentCompleted: " + ex);
                //throw; //out of bravity from Mr. Shilon, eating the error where manualresetevent is trying to synchronize the waiting between multiple threads.
            }
        }
        public void Dispose()
        {
            if (bmp != null) this.bmp.Dispose();
        }
    }
}
Posted
Updated 9-Mar-11 8:25am
v2
Comments
TweakBird 9-Mar-11 14:26pm    
Please use <pre> tag for code blocks.

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