16,553
社区成员
发帖
与我相关
我的任务
分享
'-------WebCapture模块代码-------
Module WebCaptureModel
Public Class WebCapture
Public MaxLength, ValueLength As Integer
Private WithEvents WB As New WebBrowser
Private BP As Bitmap
Public XX, YY As Integer
Private ISOK As Integer = 0
Private SPTH As String
Private BPFMT As Drawing.Imaging.ImageFormat
Private NURL As String = ""
Public Event CaptureCompleted(ByVal e As String)
Private OKOK As Boolean = True
Private Sub wb_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
On Error Resume Next
If OKOK = False Then
If BP IsNot Nothing Then BP.Dispose()
If XX = 0 Or YY = 0 Then
XX = WB.Document.Body.GetAttribute("scrollwidth")
End If
YY = WB.Document.Body.GetAttribute("scrollHeight")
WB.Size = New Point(XX, YY)
BP = New Bitmap(XX, YY)
WB.DrawToBitmap(BP, New Rectangle(0, 0, XX, YY))
If My.Computer.FileSystem.FileExists(SPTH) = True Then
Kill(SPTH)
End If
BP.Save(SPTH, BPFMT)
RaiseEvent CaptureCompleted(NURL)
OKOK = True
End If
End Sub
Public Sub StartCapture(ByVal URL As String, ByVal SavePath As String, ByVal BitmapFormat As System.Drawing.Imaging.ImageFormat)
Try
WB.ScrollBarsEnabled = False
OKOK = False
SPTH = SavePath
NURL = URL
BPFMT = BitmapFormat
WB.Navigate(URL)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub WB_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WB.ProgressChanged
MaxLength = e.MaximumProgress
ValueLength = e.CurrentProgress
End Sub
End Class
End Module
'---------功能调用代码--------
Public Class Form2
Private WithEvents CP As New WebCapture
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "正在捕获网页..."
'提供用户正在操作
Timer1.Enabled = True
'控制进度条
CP.StartCapture(TextBox1.Text, "c:\windows\temp\12312322222222.bmp", Imaging.ImageFormat.Bmp)
'开始捕获并保存为BMP格式图片
End Sub
Private Sub CP_CaptureCompleted(ByVal e As String) Handles CP.CaptureCompleted
Label1.Text = "网页捕获完成"
Shell("mspaint.exe c:\windows\temp\12312322222222.bmp", AppWinStyle.NormalFocus, False)
'打开保存的图片
Timer1.Enabled = False
'结束进度条控制
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
On Error Resume Next
PB.Maximum = CP.MaxLength
PB.Value = CP.ValueLength
End Sub
End Class