Results 1 to 3 of 3

Thread: Grabbing screen of WebBrowser1

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Grabbing screen of WebBrowser1

    I have set my webBrowser a URL and I have searched for examples but they didn't work and just return a blank screen.

    I have tried things as:
    Code:
    Private Function CreateScreenshot(ByVal Control As Control) As Bitmap
            Dim Screenshot As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
            Control.DrawToBitmap(Screenshot, New Rectangle(0, 0, WebBrowser1.Width, WebBrowser1.Height))
            Return Screenshot
        End Function
    
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim Img1 As Bitmap
            Img1 = CreateScreenshot(WebBrowser1)
            PictureBox1.Visible = True
            WebBrowser1.Visible = False
            PictureBox1.Image = Img1
        End Sub
    And others and I know how to get a screenshot but not a webBrowser screenshot.

  2. #2
    Hyperactive Member
    Join Date
    Apr 2011
    Location
    England
    Posts
    421

    Re: Grabbing screen of WebBrowser1

    The easiest way I know of is to use:
    VB.NET Code:
    1. Dim bmp As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
    2. WebBrowser1.DrawToBitmap(bmp, WebBrowser1.ClientRectangle)

    The .DrawToBitmap property will not appear in intelli-sense because the function is "not supported" but you can freely type it into your code and the function does work.

    If you want to grab the entire website including the offscreen parts, then you could scroll it between 'screenshots' or create a new WebBrowser using the page dimensions and grab the image from that.

    You could also get the Image by using Graphics.CopyFromScreen. Let me know if you would like an example. Thios way requires slightly more code but may be considered a better approach since it is "supported"

    Edit: I just realized the code snippet is pretty much what your original function is doing, so just in case here is the Graphics.CopyFromScreen method:
    VB.NET Code:
    1. 'Determine the Size of the WebBrowser region
    2. Dim wbWidth As Integer = WebBrowser1.Width
    3. Dim wbHeight As Integer = WebBrowser1.Height
    4.  
    5. 'Compensate for scrollbars if they are showing
    6. If WebBrowser1.ScrollBarsEnabled Then
    7.     If WebBrowser1.Document.Body.ScrollRectangle.Width > WebBrowser1.Width Then
    8.         wbHeight -= SystemInformation.HorizontalScrollBarHeight
    9.     End If
    10.     If WebBrowser1.Document.Body.ScrollRectangle.Height > WebBrowser1.Height Then
    11.         wbWidth -= SystemInformation.VerticalScrollBarWidth
    12.     End If
    13. End If
    14.  
    15. 'Take Screenshot
    16. Dim bmp As New Bitmap(wbWidth, wbHeight)
    17. Dim g As Graphics = Graphics.FromImage(bmp)
    18. g.CopyFromScreen(New Point(Me.PointToScreen(New Point(WebBrowser1.Left, WebBrowser1.Top))), New Point(0, 0), New Size(wbWidth, wbHeight))
    19. g.Dispose()
    20.  
    21. 'Add bitmap to picturebox
    22. PictureBox1.Image = bmp
    Last edited by JayJayson; Oct 20th, 2011 at 03:19 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    14

    Re: Grabbing screen of WebBrowser1

    The copyfromscreen worked perfectly thanks I didnt know u could set it a region cuz it kept copying the entire screen. Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width