Convert Web Page or Html to Image

This article is written by Pon Saravanan  on 25-Jun-08 Last modified on :26-Oct-09

Subscribe to my RSS Feed!

Convert Html(Web Page) to Image with Visual Studio 2008 

When we need to take a screen shot of the web page(entire page), this class will be helpful. This class converts any web page written in any programming language if it renders in browser nicely.

The title given to this article, may mislead some how technically. But it is given for understanding purposes. Actually this class navigates to the given url using the .Net 2.0's Web Browser control.

This makes this task relatively easier to get the response of the given URL. Once the navigation completed, the screen shot image can be taken with the help of GDI.
Use the DocumentCompleted event to find whether the navigation of the web page has been completed. Use a Bitmap object to draw the image.

Again taking the screenshot of the given webpage is much easier using the built in function DrawToBitmap of Web Browser component which yields a bitmap image. This image can be either saved in the hard disk or can be forced into the response stream for displaying in another webpage.

Source Code

Imports Microsoft.VisualBasic
Imports System.Threading
Imports System.Drawing
Imports System.Windows.Forms
 
Public Class ImageFromHtml
    Private PageUrl As String
    Private ConvertedImage As Bitmap
 
    Private m_intHeight As Integer
    Public Property Height() As Integer
        Get
            Return m_intHeight
        End Get
        Set(ByVal value As Integer)
            m_intHeight = value
        End Set
    End Property
 
    Private m_intWidth As Integer
    Public Property Width() As Integer
        Get
            Return m_intWidth
        End Get
        Set(ByVal value As Integer)
            m_intWidth = value
        End Set
    End Property
  
    Public Function ConvertPage(ByVal PageUrl As String) As Bitmap
        Me.PageUrl = PageUrl
        Dim thrCurrent As New Thread(New ThreadStart(AddressOf CreateImage))
        thrCurrent.SetApartmentState(ApartmentState.STA)
        thrCurrent.Start()
        thrCurrent.Join()
        Return ConvertedImage
    End Function
    Private Sub CreateImage()
 
        Dim BrowsePage As New WebBrowser()
        BrowsePage.ScrollBarsEnabled = False
        BrowsePage.Navigate(PageUrl)
        AddHandler BrowsePage.DocumentCompleted, AddressOf  _
WebBrowser_DocumentCompleted

        While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        End While
        BrowsePage.Dispose()
    End Sub
 
    Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As _
WebBrowserDocumentCompletedEventArgs)
        Dim BrowsePage As WebBrowser = DirectCast(sender, WebBrowser)
        BrowsePage.ClientSize = New Size(Width, Height)
        BrowsePage.ScrollBarsEnabled = False
        ConvertedImage = New Bitmap(Width, Height)
        BrowsePage.BringToFront()
        BrowsePage.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)
 
    End Sub
 
End Class
 










GUEST
Nice concept. Thanks for sharing. 8/12/2008 9:06:34 AM


Comments
   
Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    




Spam Bot Trap