专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > VB Dotnet

[原创分享]网页截图解决思路

发布时间:2011-06-23 16:01:38 文章来源:www.iduyao.cn 采编人员:星星草
[原创分享]网页截图
功能与说明:
自动对指定的网页进行整页截图
为方便使用,已做成单独的VB文件.
自定义图片保存地址,自定义图片格式
可获取截图进度

VB.NET code

'-------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



VB.NET code

'---------功能调用代码--------
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


功能调用演示图


最终截图演示(已缩小比例处理)


------解决方案--------------------
先研究下
------解决方案--------------------
顶!!
------解决方案--------------------
学习了
------解决方案--------------------
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: