Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View WebsiteThumbImage.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
using System;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
 
public class WebsiteThumbImage
{
public int ThumbWidth { get; set; }
public int ThumbHeight { get; set; }
public int BrowserWidth { get; set; }
public int BrowserHeight { get; set; }
 
public string Url { get; set; }
public Bitmap ThumbImage { get; set; }
 
public Bitmap Generate()
{
if (ValidationControl())
return null;
 
Thread th = new Thread(new ThreadStart(_Execute));
th.SetApartmentState(ApartmentState.STA);
th.Start();
th.Join();
 
return ThumbImage;
}
 
// just operational codes :D
public void _Execute()
{
WebBrowser browser = new WebBrowser();
browser.ScrollBarsEnabled = false;
browser.Navigate(this.Url);
browser.DocumentCompleted += browser_DocumentCompleted;
 
while (browser.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
browser.Dispose();
 
}
 
void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser WebBrowser = (WebBrowser)sender;
WebBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
WebBrowser.ScrollBarsEnabled = false;
 
this.ThumbImage = new Bitmap(WebBrowser.Bounds.Width, WebBrowser.Bounds.Height);
WebBrowser.BringToFront();
WebBrowser.DrawToBitmap(this.ThumbImage, WebBrowser.Bounds);
this.ThumbImage = (Bitmap)this.ThumbImage.GetThumbnailImage(this.ThumbWidth, this.ThumbHeight, null, IntPtr.Zero);
}
 
private bool ValidationControl()
{
if (String.IsNullOrEmpty(this.Url))
throw new ArgumentNullException();
 
// for url validation
if (!Uri.IsWellFormedUriString(this.Url, UriKind.RelativeOrAbsolute))
throw new Exception();
 
// another validation method -- using optional
if (!IsValidUrl(this.Url))
throw new Exception();
 
return true;
}
 
private bool IsValidUrl(string url)
{
return Regex.IsMatch(url, @"(http|https)://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
}
}
 
// Let's use
string url = ((string.IsNullOrEmpty(Request.Params["site"])) ? "blog.yemrekeskin.com" : Request.Params["site"]);
int width = ((string.IsNullOrEmpty(Request.Params["width"])) ? 1000 : int.Parse(Request.Params["width"]));
int height = ((string.IsNullOrEmpty(Request.Params["height"])) ? 940 : int.Parse(Request.Params["height"]));
int capWidth = ((string.IsNullOrEmpty(Request.Params["capWidth"])) ? 900 : int.Parse(Request.Params["capWidth"]));
int capHeight = ((string.IsNullOrEmpty(Request.Params["capHeight"])) ? 800 : int.Parse(Request.Params["capHeight"]));
 
string address = "http://" + url;
Bitmap thumbnail = WebsiteThumbnailImageGenerator.GetWebSiteThumbnail(address, capWidth, capHeight, width, height);
 
Response.ContentType = "image/jpeg";
thumbnail.Save(Response.OutputStream, ImageFormat.Jpeg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.