我有一个程序(目前是winform),可以加载网站,截取屏幕截图并转换为位图,然后保存。我遇到的问题是,它无法将数据加载到网站中。为什么我认为这样做,它可以快速截取屏幕截图并且没有时间完全加载。
实施例: 在“统计”下,应该有一堆似乎尚未加载的信息。
我尝试在一些地方放置Threading.sleep
,但不会改变结果。
代码
Private Sub btnDrawBmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Capture.Click
' Initialize new Hidden Browser control and navigate to site
Dim Browser As New WebBrowser
Browser.ScrollBarsEnabled = False
Browser.Navigate("http://xxxxx/LiveStats/")
' Initialize Handler for DocumentCompleted event and wait for that event to fire
AddHandler Browser.DocumentCompleted, AddressOf Browser_DocumentCompleted
While Browser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
' Housekeeping
Browser.Dispose()
End Sub
Private Sub Browser_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
' If sender was a WebBrowser then initialize a reference to it otherwise throw an exception
Dim Browser As WebBrowser = DirectCast(sender, WebBrowser)
' Set browser start size, determine scrollheight, resize browser to match scrollheight
Browser.ClientSize = New Size(1024, 768)
Dim Height As Integer = Browser.Document.Body.ScrollRectangle.Bottom
Browser.ClientSize = New Size(1024, Height)
' Initialize bitmap to match width & scrollheight for fullsize screen capture
Dim Bmp = New Bitmap(Browser.Bounds.Width, Height)
' Capture fullsize screen shot
Browser.DrawToBitmap(Bmp, Browser.Bounds)
Bmp.Save("c:\temp1.png")
' Resize Browser for thumbnail capture
Browser.ClientSize = New Size(1024, 768)
' Inialize a new bitmap with the same client area size as the browser
Dim Bmp2 = New Bitmap(Browser.Bounds.Width, Browser.Bounds.Height)
' Perform screen capture, convert it to a thumbnail, and save
Browser.DrawToBitmap(Bmp2, Browser.Bounds)
Bmp2 = Bmp2.GetThumbnailImage(160, 120, Nothing, IntPtr.Zero)
Bmp2.Save("c:\temp2.png")
' Housekeeping
Bmp.Dispose()
Bmp2.Dispose()
End Sub
更新了仍有问题的代码:
注意:表单上只有2个对象是按钮和浏览器。
Private Sub btnDrawBmp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Capture.Click
' Initialize new Hidden Browser control and navigate to site
'Dim Browser As New WebBrowser
Dim Browser = wb
Browser.ScrollBarsEnabled = False
Browser.Navigate("http://ssgw8sr01/LiveStats/")
'Browser.Navigate("http://google.com")
' Initialize Handler for DocumentCompleted event and wait for that event to fire
AddHandler Browser.DocumentCompleted, AddressOf Browser_DocumentCompleted
While Browser.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
' Housekeeping
Browser.Dispose()
End Sub
Private Sub Browser_DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
If (e.Url = wb.Url) AndAlso (wb.ReadyState = WebBrowserReadyState.Complete) Then
' If sender was a WebBrowser then initialize a reference to it otherwise throw an exception
Dim Browser As WebBrowser = DirectCast(sender, WebBrowser)
' Set browser start size, determine scrollheight, resize browser to match scrollheight
Browser.ClientSize = New Size(1024, 768)
Dim Height As Integer = Browser.Document.Body.ScrollRectangle.Bottom
Browser.ClientSize = New Size(1024, Height)
' Initialize bitmap to match width & scrollheight for fullsize screen capture
Dim Bmp = New Bitmap(Browser.Bounds.Width, Height)
' Capture fullsize screen shot
Browser.DrawToBitmap(Bmp, Browser.Bounds)
Bmp.Save("c:\temp1.png")
' Resize Browser for thumbnail capture
Browser.ClientSize = New Size(1024, 768)
' Inialize a new bitmap with the same client area size as the browser
Dim Bmp2 = New Bitmap(Browser.Bounds.Width, Browser.Bounds.Height)
' Perform screen capture, convert it to a thumbnail, and save
Browser.DrawToBitmap(Bmp2, Browser.Bounds)
Bmp2 = Bmp2.GetThumbnailImage(160, 120, Nothing, IntPtr.Zero)
Bmp2.Save("c:\temp2.png")
' Housekeeping
Bmp.Dispose()
Bmp2.Dispose()
End If
End Sub
答案 0 :(得分:2)
在doc_completed事件中添加此项,更改WB变量,我在手机上,这是PITA。
我认为您需要等待即时状态才能完成。
If (e.Url = wb.Url) AndAlso (wb.ReadyState = WebBrowserReadyState.Complete) Then
' If sender was a WebBrowser then initialize a reference to it otherwise throw an exception
Dim Browser As WebBrowser = DirectCast(sender, WebBrowser)
' Set browser start size, determine scrollheight, resize browser to match scrollheight
Browser.ClientSize = New Size(1024, 768)
Dim Height As Integer = Browser.Document.Body.ScrollRectangle.Bottom
Browser.ClientSize = New Size(1024, Height)
' Initialize bitmap to match width & scrollheight for fullsize screen capture
Dim Bmp = New Bitmap(Browser.Bounds.Width, Height)
' Capture fullsize screen shot
Browser.DrawToBitmap(Bmp, Browser.Bounds)
Bmp.Save("c:\temp1.png")
' Resize Browser for thumbnail capture
Browser.ClientSize = New Size(1024, 768)
' Inialize a new bitmap with the same client area size as the browser
Dim Bmp2 = New Bitmap(Browser.Bounds.Width, Browser.Bounds.Height)
' Perform screen capture, convert it to a thumbnail, and save
Browser.DrawToBitmap(Bmp2, Browser.Bounds)
Bmp2 = Bmp2.GetThumbnailImage(160, 120, Nothing, IntPtr.Zero)
Bmp2.Save("c:\temp2.png")
' Housekeeping
Bmp.Dispose()
Bmp2.Dispose()
End If
编辑:
这已经过测试,即使在缓慢加载的网站上也可以像魅力一样工作。在完全加载之前,它不会创建屏幕截图。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("http:\\www.msn.com")
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (e.Url = WebBrowser1.Url) AndAlso (WebBrowser1.ReadyState = WebBrowserReadyState.Complete) Then
getBitmap(WebBrowser1).Save("C:\Users\Anthony Wiles\Desktop\myScreenShot.png")
End If
End Sub
Private Function getBitmap(ByVal pCtrl As Control) As Drawing.Bitmap
Dim myBmp As New Bitmap(pCtrl.Width, pCtrl.Height)
Dim g As Graphics = Graphics.FromImage(myBmp)
Dim pt As Point = pCtrl.Parent.PointToScreen(pCtrl.Location)
g.CopyFromScreen(pt, Point.Empty, myBmp.Size)
g.Dispose()
Return myBmp
End Function