Click here to Skip to main content
15,894,017 members
Articles / Programming Languages / Visual Basic

Capture Entire Web Page

Rate me:
Please Sign up or sign in to vote.
4.56/5 (32 votes)
15 Aug 2007 164.4K   10.6K   122  
Capture an entire web page and save it as an image.
Imports System.Text.RegularExpressions

Public Class Form1

  Private Sub buttonCapture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttonCapture.Click
    WebBrowser1.Navigate(textWebURL.Text)
    buttonCapture.Enabled = False
  End Sub

  Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    GetImage()
  End Sub

  Private Sub GetImage()
    If WebBrowser1.Document Is Nothing Then
      Return
    End If
    Try
      Dim scrollWidth As Integer
      Dim scrollHeight As Integer
      scrollHeight = WebBrowser1.Document.Body.ScrollRectangle.Height
      scrollWidth = WebBrowser1.Document.Body.ScrollRectangle.Width
      WebBrowser1.Size = New Size(scrollWidth, scrollHeight)
      Dim bm As New Bitmap(scrollWidth, scrollHeight)
      WebBrowser1.DrawToBitmap(bm, New Rectangle(0, 0, bm.Width, bm.Height))
      Dim SaveAsName As String
      SaveAsName = Regex.Replace(textWebURL.Text, "(\\|\/|\:|\*|\?|\""|\<|\>|\|)?", "")
      bm.Save(SaveAsName & ".png", System.Drawing.Imaging.ImageFormat.Png)
      bm.Dispose()
    Catch ex As Exception
      MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Finally
      '
    End Try
    buttonCapture.Enabled = True
  End Sub
End Class

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States

Comments and Discussions