将WebBrowser控件绘制到位图

时间:2012-02-16 02:27:06

标签: vb.net winforms bitmap webbrowser-control drawtobitmap

我正在尝试使用以下代码(VB.net)将面板控件保存为位图:

Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
     filename = SaveFileDialog1.FileName
     Dim CardImg As New Bitmap(Panel1.Width, Panel1.Height)
     Panel1.DrawToBitmap(CardImg, Panel1.ClientRectangle)
     CardImg.Save(filename, System.Drawing.Imaging.ImageFormat.Bmp)
 End Sub

一切正常,除了停靠在面板中的Web浏览器控件。在保存的位图中,此控件仅显示为空格,而面板中的其他所有内容都显示为正常。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

当我在WebBrowser保存快照后,我在导航后调用了.Focus() - 并且不知何故白色图片的结果神奇地消失了。不知道为什么,但它对我有用。

答案 1 :(得分:-1)

http://www.vbforums.com/showthread.php?t=385497下载“ScreenCapture.vb”, 使用CaptureDeskTopRectangle但您不应该使用面板的位置,因为它是 参考面板父级,您应该使用yourpanel.PointToScreen()来识别正确的 长方形。 此致..

<强>更新

检查一下,你会喜欢它,我将你的情况简化,并且它正在发挥作用:

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Try
            Using fl As New SaveFileDialog
                fl.Filter = "PNG images|*.png"
                If fl.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Dim sc As New screencapture
                    Dim pt = WebBrowser1.Parent.PointToScreen(WebBrowser1.Location)
                    Dim rec As New Rectangle(pt.X, pt.Y, WebBrowser1.Width, WebBrowser1.Height)
                    Application.DoEvents()
                    Threading.Thread.Sleep(500)
                    Using bmp As Bitmap = sc.CaptureDeskTopRectangle(rec, WebBrowser1.Width, WebBrowser1.Height)
                        bmp.Save(fl.FileName, System.Drawing.Imaging.ImageFormat.Png)
                    End Using
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

更新2:

表格:

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Try
            Using fl As New SaveFileDialog
                fl.Filter = "PNG images|*.png"
                If fl.ShowDialog = Windows.Forms.DialogResult.OK Then
                    JSsetTimeout.SetTimeout(Me, "TakeShot", 1500, fl.FileName)
                End If
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
End Sub

Sub TakeShot(ByVal FilePath As String)
        Try
            Application.DoEvents()
            Dim sc As New screencapture
            Dim pt = WebBrowser1.Parent.PointToScreen(WebBrowser1.Location)
            Dim rec As New Rectangle(pt.X, pt.Y, WebBrowser1.Width, WebBrowser1.Height)
            Using bmp As Bitmap = sc.CaptureDeskTopRectangle(rec, WebBrowser1.Width, WebBrowser1.Height)
                bmp.Save(FilePath, System.Drawing.Imaging.ImageFormat.Png)
            End Using
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
End Sub

要创建延时,请添加以下课程:

Public Class JSsetTimeout

    Public res As Object = Nothing
    Dim WithEvents tm As Timer = Nothing
    Dim _MethodName As String
    Dim _args() As Object
    Dim _ClassInstacne As Object = Nothing

    Public Shared Sub SetTimeout(ByVal ClassInstacne As Object, ByVal obj As String, ByVal TimeSpan As Integer, ByVal ParamArray args() As Object)
        Dim jssto As New JSsetTimeout(ClassInstacne, obj, TimeSpan, args)
    End Sub

    Public Sub New(ByVal ClassInstacne As Object, ByVal obj As String, ByVal TimeSpan As Integer, ByVal ParamArray args() As Object)
        If obj IsNot Nothing Then
            _MethodName = obj
            _args = args
            _ClassInstacne = ClassInstacne
            tm = New Timer With {.Interval = TimeSpan, .Enabled = False}
            AddHandler tm.Tick, AddressOf tm_Tick
            tm.Start()
        End If
    End Sub

    Private Sub tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tm.Tick
        tm.Stop()
        RemoveHandler tm.Tick, AddressOf tm_Tick
        If Not String.IsNullOrEmpty(_MethodName) AndAlso _ClassInstacne IsNot Nothing Then
            res = CallByName(_ClassInstacne, _MethodName, CallType.Method, _args)
        Else
            res = Nothing
        End If
    End Sub
End Class