Click here to Skip to main content
12,396,461 members (34,586 online)
Click here to Skip to main content

Stats

91.1K views
5.5K downloads
70 bookmarked
Posted

Get ASP.NET C# 2.0 Website Thumbnail Screenshot

, 22 Jul 2010 CPOL
How to get a Website/URL thumbnail/screenshot with C#.NET 2.0 in VS 2005.
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);

                __thread.SetApartmentState(ApartmentState.STA);
                __thread.Start();
                __thread.Join();
                return __Bitmap;
            }

            private void _GenerateWSThumb()            {
                WebBrowser __WebBrowser = new WebBrowser();
                __WebBrowser.ScrollBarsEnabled = false;
                __WebBrowser.Navigate(__Url);
                __WebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
                while (__WebBrowser.ReadyState != WebBrowserReadyState.Complete)
                    Application.DoEvents();
                __WebBrowser.Dispose();
            }

            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.BringToFront();
                __WebBrowser.DrawToBitmap(__Bitmap, __WebBrowser.Bounds);
                
                if (__ThumbnailHeight != 0 && __ThumbnailWidth !=0)
                __Bitmap = (Bitmap)__Bitmap.GetThumbnailImage(__ThumbnailWidth, __ThumbnailHeight, null, IntPtr.Zero);                
            }
        }
    }
}


By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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

Share

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...

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