using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Forms;
public partial class snapshot : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = ((string.IsNullOrEmpty(Request.Params["site"])) ? "www.microsoft.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"]));
int capWidth = ((string.IsNullOrEmpty(Request.Params["capWidth"])) ? 1024 : int.Parse(Request.Params["capWidth"]));
int capHeight = ((string.IsNullOrEmpty(Request.Params["capHeight"])) ? 768 : int.Parse(Request.Params["capHeight"]));
bool fullHeight = ((string.IsNullOrEmpty(Request.Params["fullHeight"])) ? true : bool.Parse(Request.Params["fullHeight"]));
string address = "http://" + url.Replace("hhtp://","");
Bitmap thumbnail = WebsiteThumbnailImageGenerator.GetWebSiteThumbnail(address, capWidth, capHeight, width, height,fullHeight);
Response.ContentType = "image/jpeg";
thumbnail.Save (Response.OutputStream, ImageFormat.Jpeg);
}
}
public class WebsiteThumbnailImageGenerator
{
public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight, bool FullHeight)
{
WebsiteThumbnailImage thumbnailGenerator = new WebsiteThumbnailImage(Url, BrowserWidth, BrowserHeight, ThumbnailWidth, ThumbnailHeight,FullHeight);
return thumbnailGenerator.GenerateWebSiteThumbnailImage();
}
private class WebsiteThumbnailImage
{
private WebBrowser _blankBrowser = null;
public WebsiteThumbnailImage(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight, bool FullHeight)
{
this.m_Url = Url;
this.m_BrowserWidth = BrowserWidth;
this.m_BrowserHeight = BrowserHeight;
this.m_ThumbnailHeight = ThumbnailHeight;
this.m_ThumbnailWidth = ThumbnailWidth;
this.m_fullHeight = FullHeight;
//_blankBrowser = new WebBrowser();
}
private string m_Url = null;
public string Url
{
get
{
return m_Url;
}
set
{
m_Url = value;
}
}
private Bitmap m_Bitmap = null;
public Bitmap ThumbnailImage
{
get
{
return m_Bitmap;
}
}
private int m_ThumbnailWidth;
public int ThumbnailWidth
{
get
{
return m_ThumbnailWidth;
}
set
{
m_ThumbnailWidth = value;
}
}
private int m_ThumbnailHeight;
public int ThumbnailHeight
{
get
{
return m_ThumbnailHeight;
}
set
{
m_ThumbnailHeight = value;
}
}
private int m_BrowserWidth;
public int BrowserWidth
{
get
{
return m_BrowserWidth;
}
set
{
m_BrowserWidth = value;
}
}
private int m_BrowserHeight;
public int BrowserHeight
{
get
{
return m_BrowserHeight;
}
set
{
m_BrowserHeight = value;
}
}
private bool m_fullHeight;
public bool FullHeight
{
get { return m_fullHeight;}
set { m_fullHeight = value;}
}
public Bitmap GenerateWebSiteThumbnailImage()
{
Thread m_thread = new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
m_thread.SetApartmentState(ApartmentState.STA);
m_thread.Start();
m_thread.Join();
return m_Bitmap;
}
private void _GenerateWebSiteThumbnailImage()
{
WebBrowser m_WebBrowser = new WebBrowser();
m_WebBrowser.ScrollBarsEnabled = false;
m_WebBrowser.Navigate(m_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;
int _height = this.m_BrowserHeight;
int _width = this.m_BrowserWidth;
Size sz = m_WebBrowser.Size;
if (this.FullHeight)
{
_height = m_WebBrowser.Document.Body.ScrollRectangle.Height;
_width = m_WebBrowser.Document.Body.ScrollRectangle.Width;
}
if (_width < this.BrowserWidth)
_width = this.BrowserWidth;
if (_height < sz.Height)
_height = sz.Height;
if (_width != m_WebBrowser.Width)
{
m_WebBrowser.Size = new Size(_width, _height);
//_blankBrowser.Size = new Size(_width, _height);
m_WebBrowser.Refresh(WebBrowserRefreshOption.Completely);
if (this.FullHeight)
{
_height = m_WebBrowser.Document.Body.ScrollRectangle.Height + SystemInformation.VerticalScrollBarWidth;
}
if (_height < sz.Height)
_height = sz.Height;
}
m_Bitmap = new Bitmap(_width, _height);
m_WebBrowser.DrawToBitmap(m_Bitmap, new Rectangle(m_WebBrowser.Location.X, m_WebBrowser.Location.Y, m_WebBrowser.Width, m_WebBrowser.Height));
PrepareThumbNailSize(_height, _width);
m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);
}
private void PrepareThumbNailSize(int OriginalHeight, int OriginalWidth)
{
if (this.FullHeight)
{
double factor = Convert.ToDouble(OriginalHeight) / Convert.ToDouble(OriginalWidth);
double calc = Convert.ToDouble(this.m_ThumbnailWidth) * factor;
int thumbHeight = Convert.ToInt32(calc);
this.m_ThumbnailHeight = thumbHeight;
}
}
}
}