Click here to Skip to main content
11,931,911 members (63,408 online)
Click here to Skip to main content
Add your own
alternative version


70 bookmarked

Get ASP.NET C# 2.0 Website Thumbnail Screenshot

, 22 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
How to get a Website/URL thumbnail/screenshot with C#.NET 2.0 in VS 2005.


Sample application screenshot


This sample code/website gets the thumbnail/screenshot for a specific URL, giving the browser dimensions and the thumbnail dimensions you want.


Well, tired of search for a method for giving me the screenshot of a URL, and just finding examples of Windows.Forms, with the simple method DrawToBitmap, I just realized that in ASP.NET, that does not work like that, because of Single Thread and Multi Thread; so I used .SetApartmentState(ApartmentState.STA) to make it single threaded.

Using the Code

Just call the method that you want to associate with the ImageUrl of an ASP.NET Image control.


Calling with the IHttpHandler Method

ImageBox.ImageUrl = "~/HandlerWSThumb.ashx?url=" + 
   Server.UrlEncode(txtURL.Text).ToLower().ToString() + "&bw=" + 
   txtBW.Text + "&bh=" + txtBH.Text + "&tw=" + 
   txtTW.Text + "&th=" + txtTH.Text;

Calling with the Class Method

Bitmap bmp = ClassWSThumb.GetWebSiteThumbnail(txtURL.Text, Int32.Parse(txtBW.Text), 
             Int32.Parse(txtBH.Text), Int32.Parse(txtTW.Text), 
bmp.Save(Server.MapPath("~") + "/thumbnail.bmp");
ImageBox.ImageUrl = "~/thumbnail.bmp";

//**if Jpeg is the image format you need just replace the 2 lines above for:
//bmp.Save(Server.MapPath("~") + "/thumbnail.jpg", ImageFormat.Jpeg);
//ImageBox.ImageUrl = "~/thumbnail.jpg";

//** but don't forget to add the reference
//   to the class in the page 'Default.aspx'(code behind):
//using System.Drawing.Imaging;

Here is the class method code:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace GetWebSiteThumb
    public class ClassWSThumb
        public static Bitmap GetWebSiteThumbnail(string Url, int BrowserWidth, 
               int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
            return new WSThumb(Url, BrowserWidth, BrowserHeight, 
                   ThumbnailWidth, ThumbnailHeight).GetWSThumb();

        private class WSThumb
            public WSThumb(string Url, int BW, int BH, int TW, int TH) {
                __Url = Url;
                __BrowserWidth = BW;
                __BrowserHeight = BH;
                __ThumbnailWidth = TW;
                __ThumbnailHeight = TH;

            private Bitmap __Bitmap = null;            
            private string __Url = null;
            private int __ThumbnailWidth;
            private int __ThumbnailHeight;
            private int __BrowserWidth;
            private int __BrowserHeight;

            public string Url            {
                get{return __Url;}
                set{__Url = value;}
            public Bitmap ThumbnailImage            {
                get{return __Bitmap;}

            public int ThumbnailWidth            {
                get{return __ThumbnailWidth;}
                set{__ThumbnailWidth = value;}

            public int ThumbnailHeight            {
                get {return __ThumbnailHeight;}
                set {__ThumbnailHeight = value;}

            public int BrowserWidth            {
                get{return __BrowserWidth;}
                set{__BrowserWidth = value;}

            public int BrowserHeight            {
                get{return __BrowserHeight;}
                set{__BrowserHeight = value;}

            public Bitmap GetWSThumb()            {
                ThreadStart __threadStart = new ThreadStart(_GenerateWSThumb);
                Thread __thread = new Thread(__threadStart);

                return __Bitmap;

            private void _GenerateWSThumb()            {
                WebBrowser __WebBrowser = new WebBrowser();
                __WebBrowser.ScrollBarsEnabled = false;
                __WebBrowser.DocumentCompleted += 
                  new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                while (__WebBrowser.ReadyState != WebBrowserReadyState.Complete)

            private void WebBrowser_DocumentCompleted(object sender, 
                         WebBrowserDocumentCompletedEventArgs e)            {
                WebBrowser __WebBrowser = (WebBrowser)sender;
                __WebBrowser.ClientSize = new Size(this.__BrowserWidth, this.__BrowserHeight);
                __WebBrowser.ScrollBarsEnabled = false;
                __Bitmap = new Bitmap(__WebBrowser.Bounds.Width, __WebBrowser.Bounds.Height);
                __WebBrowser.DrawToBitmap(__Bitmap, __WebBrowser.Bounds);
                if (__ThumbnailHeight != 0 && __ThumbnailWidth !=0)
                __Bitmap = (Bitmap)__Bitmap.GetThumbnailImage(
                    __ThumbnailWidth, __ThumbnailHeight, null, IntPtr.Zero);                

Here is the IHTTPHandler method code:

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Reflection;

namespace GetWebSiteThumb
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class HandlerWSThumb : IHttpHandler
        public void ProcessRequest(HttpContext context)
            Bitmap thumb = null;
            string url = null;
            int bw = 800; //valor por defeito
            int bh = 600; //valor por defeito
            int tw = 0;    // sem thumbnail 
            int th = 0;

            context.Response.ContentType = "image/jpeg";
            if (context.Request["url"] != null)
                if (context.Request["url"].ToString().ToLower().Contains("http://") || 
                    url = context.Request["url"].ToString();
                    url = "http://" + context.Request["url"].ToString();

            if (context.Request["bw"] != null)
                bw = Int32.Parse(context.Request["bw"].ToString());

            if (context.Request["bh"] != null)
                bh = Int32.Parse(context.Request["bh"].ToString());

            if (context.Request["tw"] != null)
                tw = Int32.Parse(context.Request["tw"].ToString());

            if (context.Request["th"] != null)
                th = Int32.Parse(context.Request["th"].ToString());

            // return context bitmap
            thumb = GetWebSiteThumbnail(url, bw, bh);

            if (tw != 0 && th != 0)
                thumb.GetThumbnailImage(tw, th, null, IntPtr.Zero).Save(
                           context.Response.OutputStream, ImageFormat.Jpeg);
                thumb.Save(context.Response.OutputStream, ImageFormat.Jpeg);


        public static Bitmap GetWebSiteThumbnail(string Url, 
                      int BrowserWidth, int BrowserHeight)
            WebsiteThumbnailImage thumbnailGenerator = 
              new WebsiteThumbnailImage(Url, BrowserWidth, BrowserHeight);
            return thumbnailGenerator.GenerateWebSiteThumbnailImage();

        private class WebsiteThumbnailImage
            public WebsiteThumbnailImage(string Url, 
                   int BrowserWidth, int BrowserHeight)
                this.m_Url = Url;
                this.m_BrowserWidth = BrowserWidth;
                this.m_BrowserHeight = BrowserHeight;

            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_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; }

            public Bitmap GenerateWebSiteThumbnailImage()
                Thread m_thread = 
                  new Thread(new ThreadStart(_GenerateWebSiteThumbnailImage));
                return m_Bitmap;

            private void _GenerateWebSiteThumbnailImage()
                WebBrowser m_WebBrowser = new WebBrowser();
                m_WebBrowser.ScrollBarsEnabled = false;
                m_WebBrowser.DocumentCompleted += 
                  new WebBrowserDocumentCompletedEventHandler(
                while (m_WebBrowser.ReadyState != WebBrowserReadyState.Complete)

            private void WebBrowser_DocumentCompleted(object sender, 
                    WebBrowserDocumentCompletedEventArgs e)
                WebBrowser m_WebBrowser = (WebBrowser)sender;
                m_WebBrowser.ClientSize = new Size(this.m_BrowserWidth, 
                m_WebBrowser.ScrollBarsEnabled = false;
                m_Bitmap = new Bitmap(m_WebBrowser.Bounds.Width, 
                m_WebBrowser.DrawToBitmap(m_Bitmap, m_WebBrowser.Bounds);
                //m_Bitmap = (Bitmap)m_Bitmap.GetThumbnailImage(
                //   m_ThumbnailWidth, m_ThumbnailHeight, null, IntPtr.Zero);

        // Resusable flag
        public bool IsReusable
            get { return false; }

Points of Interest

I learn that there are always three ways to do things: the right way, the wrong way, and the best way. I tried to cover the 3 of them Wink | ;) As an add-on, you could build a WebService method for returning the thumbnail. Maybe I will edit this article in the near future and put some code in the sample to accomplish it.


  • Version 1.0: Upgrades and tips are welcome.
  • Version 1.1: Just added a comment about converting the resulting BMP in the class method to JPEG, after a user asked me to do that.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gilberto Francisco
Web Developer Microsky
Portugal Portugal
Software developer since 1986, as a hobby
and since 1998 as a professional

Currently working for Ogilvy S.A.
(Advertinsing media in portugal)

You may also be interested in...

Comments and Discussions

QuestionCode is working on local but not on live. i am getting error in console "net::ERR_CONNECTION_RESET" Pin
Jahangir Shaikh16-Sep-15 0:46
memberJahangir Shaikh16-Sep-15 0:46 
Questionhow can we increase performance of code. Pin
Santosh K. Tripathi28-Jul-15 2:49
professionalSantosh K. Tripathi28-Jul-15 2:49 
QuestionThanks bro! Pin
Member 923658523-Jun-14 6:39
memberMember 923658523-Jun-14 6:39 
GeneralNice Solution! Pin
johnh12325-Apr-14 9:40
memberjohnh12325-Apr-14 9:40 
QuestionUnable to get google screenshot when project is deployed on IIS Pin
Abid Sayed9-Sep-13 19:57
memberAbid Sayed9-Sep-13 19:57 
AnswerRe: Unable to get google screenshot when project is deployed on IIS Pin
Gilberto Francisco5-Feb-14 11:13
memberGilberto Francisco5-Feb-14 11:13 
GeneralMy vote of 3 Pin
ref99715-Jan-13 3:24
memberref99715-Jan-13 3:24 
GeneralMy vote of 5 Pin
Savalia Manoj M30-Dec-12 22:52
memberSavalia Manoj M30-Dec-12 22:52 
Questionwant to capture entire webpage with scrollbar Pin
Gaurav Mishra29-Nov-12 3:54
memberGaurav Mishra29-Nov-12 3:54 
AnswerRe: want to capture entire webpage with scrollbar Pin
Gilberto Francisco29-Nov-12 4:37
memberGilberto Francisco29-Nov-12 4:37 
Questionnot working for dynamic webpages Pin
roopali@241127-Aug-12 4:30
memberroopali@241127-Aug-12 4:30 
NewsWorks well, Vote 5 Pin
DadajiIn15-Jun-12 1:30
memberDadajiIn15-Jun-12 1:30 
Suggestionsome suggestions Pin
Member 43531606-Apr-12 1:56
memberMember 43531606-Apr-12 1:56 
QuestionAwesome article Pin
TanzeelurRehman13-Mar-12 8:59
memberTanzeelurRehman13-Mar-12 8:59 
GeneralI managed to convert the class ClassWSThumb in and the usage of it in any click button event Pin
temhz14-Dec-11 7:33
membertemhz14-Dec-11 7:33 
QuestionIt can't change the Image name . [modified] Pin
Hatef Ghaniyarlou10-Aug-11 5:49
memberHatef Ghaniyarlou10-Aug-11 5:49 
AnswerRe: It can't change the Image name . [modified] Pin
moshman0811-Nov-11 8:52
membermoshman0811-Nov-11 8:52 
GeneralMy publish code not working Pin
NibeditaJena13-Apr-11 2:18
memberNibeditaJena13-Apr-11 2:18 ? Pin
Member 777093220-Mar-11 21:25
memberMember 777093220-Mar-11 21:25 
Generaldoes not work Pin
Omar Gamil14-Mar-11 4:12
memberOmar Gamil14-Mar-11 4:12 
GeneralRe: does not work Pin
Gilberto Francisco14-Mar-11 5:55
memberGilberto Francisco14-Mar-11 5:55 
GeneralRe: does not work Pin
Omar Gamil14-Mar-11 5:59
memberOmar Gamil14-Mar-11 5:59 
GeneralRe: does not work Pin
Gilberto Francisco14-Mar-11 6:05
memberGilberto Francisco14-Mar-11 6:05 
GeneralRe: does not work Pin
Omar Gamil14-Mar-11 6:07
memberOmar Gamil14-Mar-11 6:07 
GeneralRe: does not work Pin
Gilberto Francisco14-Mar-11 6:44
memberGilberto Francisco14-Mar-11 6:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151126.1 | Last Updated 22 Jul 2010
Article Copyright 2010 by Gilberto Francisco
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid