|
-
Oct 20th, 2011, 02:24 PM
#1
Thread Starter
New Member
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.
-
Oct 20th, 2011, 03:05 PM
#2
Hyperactive Member
Re: Grabbing screen of WebBrowser1
The easiest way I know of is to use:
VB.NET Code:
Dim bmp As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
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:
'Determine the Size of the WebBrowser region
Dim wbWidth As Integer = WebBrowser1.Width
Dim wbHeight As Integer = WebBrowser1.Height
'Compensate for scrollbars if they are showing
If WebBrowser1.ScrollBarsEnabled Then
If WebBrowser1.Document.Body.ScrollRectangle.Width > WebBrowser1.Width Then
wbHeight -= SystemInformation.HorizontalScrollBarHeight
End If
If WebBrowser1.Document.Body.ScrollRectangle.Height > WebBrowser1.Height Then
wbWidth -= SystemInformation.VerticalScrollBarWidth
End If
End If
'Take Screenshot
Dim bmp As New Bitmap(wbWidth, wbHeight)
Dim g As Graphics = Graphics.FromImage(bmp)
g.CopyFromScreen(New Point(Me.PointToScreen(New Point(WebBrowser1.Left, WebBrowser1.Top))), New Point(0, 0), New Size(wbWidth, wbHeight))
g.Dispose()
'Add bitmap to picturebox
PictureBox1.Image = bmp
Last edited by JayJayson; Oct 20th, 2011 at 03:19 PM.
-
Oct 20th, 2011, 03:27 PM
#3
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|