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:
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;
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()
{
try
{
log.Debug("GetScreenShot begins: ");
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);
}
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);
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);
img.Save(fullfilePath);
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);
}
}
public void Dispose()
{
if (bmp != null) this.bmp.Dispose();
}
}
}