Results 1 to 1 of 1

Thread: Capture image of WebBrowser Control

  1. #1

    Thread Starter
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    72

    Question Capture image of WebBrowser Control

    Using the following code...

    Code:
        
        Private Sub ButtonTakeImage_Click(sender As Object, e As EventArgs) Handles ButtonTakeImage.Click
            TakeScreenShot(FormEditor.WebBrowser).Save("template-image.png", System.Drawing.Imaging.ImageFormat.Png)
            PictureBoxTemplate.Image = Image.FromFile("template-image.png")
        End Sub
    
        Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
            Dim tmpImg As New Bitmap(Control.Width, Control.Height)
            Using g As Graphics = Graphics.FromImage(tmpImg)
                g.CopyFromScreen(FormEditor.WebBrowser.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(FormEditor.WebBrowser.Width, FormEditor.WebBrowser.Height))
            End Using
            Return tmpImg
        End Function
    I've tried capturing just the browser control as well as the panel that it is on, and it saves either a blank image or an image of the what's on the screen above the control.
    How do I capture an image of the browser and not the browser area and what's on top of it.

    I should explain that I am calling this capture from another form.

    EDIT: I'm able to solve the issue by manipulating the forms at run time, hiding the form that calls for the image grab and bringing the browser form to front before the capture and using this code:
    Code:
        Private Sub ButtonTakeImage_Click(sender As Object, e As EventArgs) Handles ButtonTakeImage.Click
            FormEditor.Enabled = True
            FormEditor.TopMost = True
            FormEditor.Refresh()
            Dim bmp As New Bitmap(FormEditor.WebBrowser.Width, FormEditor.WebBrowser.Height)
            Dim g As Graphics = Graphics.FromImage(bmp)
            Dim Browserlocation As New Point(FormEditor.SplitContainerMain.SplitterDistance, 62) ' location of the browser window on the form.
            Dim p As Point = FormEditor.PointToScreen(Browserlocation)
            g.CopyFromScreen(p.X, p.Y, 0, 0, bmp.Size)
            PictureBoxTemplate.Image = bmp
            FormEditor.Enabled = False
            FormEditor.TopMost = False
            Me.TopMost = True
        End Sub
    Last edited by VisualBrian; Dec 20th, 2020 at 04:15 PM.
    I may not know anything, but I know it well!

Tags for this Thread

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