Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15571

VS 2019 Capture image of WebBrowser Control

$
0
0
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


Viewing all articles
Browse latest Browse all 15571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>