Thursday, September 09, 2010

How to convert Html page to a Image using WebBrowser in

Thumbnail Image:
public class Thumbnail
    public string Url { getset; }
    public Bitmap ThumbnailImage { getset; }
    public int Width { getset; }
    public int Height { getset; }
    public int BrowserWidth { getset; }
    public int BrowserHeight { getset; }
    public Thumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        this.Url = Url;
        this.BrowserWidth = BrowserWidth;
        this.BrowserHeight = BrowserHeight;
        this.Height = ThumbnailHeight;
        this.Width = ThumbnailWidth;
    public Bitmap GenerateThumbnail()
        Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
        return ThumbnailImage;
    private void GenerateThumbnailInteral()
        WebBrowser webBrowser = new WebBrowser();
        webBrowser.ScrollBarsEnabled = false;
        webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
        while (webBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents();
    private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        WebBrowser webBrowser = (WebBrowser)sender;
        webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
        webBrowser.ScrollBarsEnabled = false;
        this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
        webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
        this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, nullIntPtr.Zero);
Test Page:
<%@ Page Language="C#" %>
<html xmlns="">
<head id="Head1" runat="server">
    <script runat="server">
        protected void CreateThumbnailImage(object sender, EventArgs e)
            string url = string.Format("http://{0}", txtWebsiteAddress.Text);
            int width = Int32.Parse(txtWidth.Text);
            int height = Int32.Parse(txtHeight.Text);
            Thumbnail thumbnail = new Thumbnail(url, 800, 600, width, height);
            Bitmap image = thumbnail.GenerateThumbnail();
            image.Save(Server.MapPath("~") + "/Thumbnail.bmp");
            imgThumbnailImage.ImageUrl = "~/Thumbnail.bmp";
            imgThumbnailImage.Visible = true;
    <form id="form1" runat="server">
        <asp:TextBox ID="txtWebsiteAddress" runat="server" Text="" /> 
        <asp:Button ID="btnCreateThumbnailImage" runat="server" Text="Create Thumbnail Image" OnClick="CreateThumbnailImage" /></td>
        <asp:TextBox ID="txtWidth" runat="server" Text="200" />
        <asp:TextBox ID="txtHeight" runat="server" Text="200" />
        <asp:Image ID="imgThumbnailImage" runat="server" Visible="false" />


Sagar Singh said...

Hey the article is kool but there us a small problem..the web browser control will work with inter explore can i do the same with other browser.Any idea?

Mohammad Fawaz said...

Thanks for you


Anonymous said...

Works great if not flash on target page ;)

Jonatan Machado said...


Anonymous said...

How can I convert a div with id='divprint' as image and print the image using this code? I want to print the div content without any margin, header or footer.

Thank You.

Charith Shyam Gunasekara said...

But unfortunately you have to load the div in to separate HTML page in order to get it printed. or else, load the page with specific style sheet where it hide all the other stuff except the required div you are interested.

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...