Thursday, 28 January 2010

Take website screenshot using ASP.NET

Utilising a hidden web browser control it is possible to take a screenshot of any website. The code shown below is based on an article at (sorry the site now appears to be offline May 2012) but I have translated it from VB.NET to C# and will work in .NET so theoretically for any Windows or ASP.NET web project.
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Diagnostics;

namespace WebsiteScreenshot
    public class GetImage
        private int s_Height; private int s_Width; private int f_Height; private int f_Width; private string myURL;

        public int ScreenHeight { get { return s_Height; } set { s_Height = value; } }
        public int ScreenWidth { get { return s_Width; } set { s_Width = value; } }
        public int ImageWidth { get { return f_Width; } set { f_Width = value; } }
        public int ImageHeight { get { return f_Height; } set { f_Height = value; } }
        public string Website { get { return myURL; } set { myURL = value; } }

        public GetImage(string website, int screenWidth, int screenHeight, int imageWidth, int imageHeight)
            myURL = website; 
            s_Width = screenHeight; 
            s_Height = screenHeight; 
            f_Height = imageHeight; 
            f_Width = imageWidth;

        public Bitmap GetBitmap()
            WebPageBitmap shot = new WebPageBitmap(this.Website, this.ScreenWidth, this.ScreenHeight); shot.GetIt();
            Bitmap pic = shot.DrawBitmap(this.ImageHeight, this.ImageWidth);
            return pic;

    class WebPageBitmap
        WebBrowser myBrowser; string url; int height; int width;

        public WebPageBitmap(string url, int width, int height)
            this.height = height; this.width = width; this.url = url;
            myBrowser = new WebBrowser(); myBrowser.ScrollBarsEnabled = false; myBrowser.Size = new Size(this.width, this.height);

        public void GetIt()
            while (myBrowser.ReadyState != WebBrowserReadyState.Complete)

        public Bitmap DrawBitmap(int theight, int twidth)
            Bitmap myBitmap = new Bitmap(width, height); Rectangle drawRect = new Rectangle(0, 0, width, height);

            myBrowser.DrawToBitmap(myBitmap, drawRect);

            Image imgOutput = myBitmap; Image oThumbnail = new Bitmap(twidth, theight, imgOutput.PixelFormat); Graphics g = Graphics.FromImage(oThumbnail); g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;

            Rectangle oRectangle = new Rectangle(0, 0, twidth, theight); g.DrawImage(imgOutput, oRectangle);

                return new Bitmap(oThumbnail); 
            catch (Exception ex) 
                return null; 
                imgOutput = null; 
                myBrowser = null; 


RogerB said...

Why do people plagiarise other people's code and claim that they have achieved anything?

Btw, non of the links work, nor for the original article that was borrowed, or for your resource.

Slogicus said...

I am also puzzled why people pass off other people's work as their own. I'm also puzzled why RogerB chose to make this comment on a post that clearly credits the source site and states that the code has simply been converted to C# - there is no claim of the code being Banshee Tech's own.

The post dates from early 2010, the links were spotted as not working in late 2011 - hardly surprising as sites come and go.