Hi,
In this article i'll give you the solution in order to generate WebSite Snapshot thumbnails with C# using the ImageThumbnailer class from my previous post.
To Get WebSite snapshot I use a WebBrowser component from System.Windows.Forms.
Here is the WebSiteSnapShot class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;
namespace WebSiteSnapshotThumbnails
{
public class WebSiteSnapshot
{
#region Private Properties
private String Url { get; set; }
private Int32 BrowserWidth { get; set; }
private Int32 BrowserHeight { get; set; }
private Bitmap Bitmap { get; set; }
#endregion
#region Constructors
public WebSiteSnapshot(string url, int browserWidth, int browserHeight)
{
this.Url = url;
this.BrowserWidth = browserWidth;
this.BrowserHeight = browserHeight;
}
#endregion
#region Public Methods
public Bitmap GenerateWebSiteImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return Bitmap;
}
#endregion
#region Private Methods
private void _GenerateWebSiteImage()
{
WebBrowser m_WebBrowser = new WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate(Url);
m_WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
m_WebBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser m_WebBrowser = (WebBrowser)sender;
m_WebBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
m_WebBrowser.ScrollBarsEnabled = false;
Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, m_WebBrowser.Bounds.Height);
m_WebBrowser.BringToFront();
m_WebBrowser.DrawToBitmap(Bitmap, m_WebBrowser.Bounds);
}
#endregion
}
}
Here is the HttpHandler using the WebSiteSnapShot class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
namespace WebSiteSnapshotThumbnails
{
public class SnapWebSite : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse Response = context.Response;
HttpRequest Request = context.Request;
string url = ((string.IsNullOrEmpty(Request.Params["site"]))
? "www.google.com" : Request.Params["site"]);
int width = ((string.IsNullOrEmpty(Request.Params["width"]))
? 200 : int.Parse(Request.Params["width"]));
int height = ((string.IsNullOrEmpty(Request.Params["height"]))
? 140 : int.Parse(Request.Params["height"]));
WebSiteSnapshot snapshot = new WebSiteSnapshot(url, 800, 600);
Bitmap original = snapshot.GenerateWebSiteImage();
ImageThumbnailer thumbnailer = new ImageThumbnailer();
thumbnailer.CreateThumbnail(original, width, height, true);
Response.ContentType = "image/jpeg";
thumbnailer.Save(Response.OutputStream);
}
public bool IsReusable
{
get { return false; }
}
}
}
You can use it by calling the Handler as the following URL :
http://localhost/SnapWebSite.ashx?site=http://blog.sb2.fr&width=200&height=140
Hope this help's!
Stay Tuned!
Views(5185)

