在 Vb.net 中打印 GeckoWebBrowser 控件

Printing GeckoWebBrowser Control in Vb.net

我正在 vb.net winform 中使用 GeckoWebBrowser 控件,我想将页面内容直接打印到默认打印机。

我找不到帮助 material 所以我试图截取该页面的屏幕截图并打印,但它在第一页之后就丢失了。 我正在使用 MicrosoftPowerPack 库。 下面是我尝试打印页面的代码。

    Dim settings As New System.Drawing.Printing.PrinterSettings
    PrintForm1.PrinterSettings = settings
    settings.DefaultPageSettings.Landscape = True
    PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.CompatibleModeFullWindow)

截图功能为:

Private Function TakeScreenShot(ByVal Control As Control) As Bitmap
    Dim tmpImg As New Bitmap(Control.Width, Control.Height)
    Using g As Graphics = Graphics.FromImage(tmpImg)
        g.CopyFromScreen(Control.PointToScreen(New Point(0, 0)), New Point(0, 0), New Size(Control.Width, Control.Height))
    End Using
    Return tmpImg
End Function

用法:TakeScreenShot(WebBroswer1).Save("C:\Users\user1\Desktop\test.png", System.Drawing.Imaging.ImageFormat.Png)

WebBroswer1 替换为您的 GeckoWebBroswer,将 C:\Users\user1\Desktop\test.png 替换为您的图片路径。

然后就可以打印图像了。

此代码将页面输出到 png 文件:(尽管它很慢并且它会在程序 运行 时冻结您的程序。尝试将其放入后台工作程序以避免冻结)

它很慢,因为它保存了非常高分辨率的图像。不过要看你的网速了。

将其放在代码的最顶部:

Imports System.Net
Imports System.Text
Imports System.IO

子是:

Dim logincookie As CookieContainer
Public Sub urltoimage(ByVal url As String, ByVal pth As String)
    Dim postdata As String = "websiteurl=" & url & "&filetype=PNG&source=WEENYSOFT&convert=Convert+Now%21"
    Dim tempCookies As New CookieContainer
    Dim encoding As New UTF8Encoding
    Dim byteData As Byte() = encoding.GetBytes(postdata)
    Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("http://s2.pdfconvertonline.com/convert/convert-webpage-win.php"), HttpWebRequest)
    postReq.Method = "POST"
    postReq.KeepAlive = True
    postReq.CookieContainer = tempCookies
    postReq.ContentType = "application/x-www-form-urlencoded"
    postReq.Referer = "http://s2.pdfconvertonline.com/convert/convert-webpage-win.php"
    postReq.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
    postReq.ContentLength = byteData.Length
    Dim postreqstream As Stream = postReq.GetRequestStream
    postreqstream.Write(byteData, 0, byteData.Length)
    postreqstream.Close()
    Dim postresponse As HttpWebResponse
    postresponse = DirectCast(postReq.GetResponse, HttpWebResponse)
    tempCookies.Add(postresponse.Cookies)
    logincookie = tempCookies
    Dim postreqreader As New StreamReader(postresponse.GetResponseStream)
    Dim thepage As String = postreqreader.ReadToEnd
    Dim tb As New TextBox
    tb.Text = thepage
    For Each l In tb.Lines
        If l.Contains("pdfconvertonline.com/convert/") AndAlso l.Contains(".png") AndAlso l.Contains("http://") Then
            Dim i As Integer = l.IndexOf("http://")
            Dim f As String = "h" & l.Substring(i + 1, l.IndexOf("""", i + 1) - i - 1).Replace(" ", "")
            My.Computer.Network.DownloadFile(f, pth)
        End If
    Next
End Sub

例如。 urltoimage("www.whosebug.com", "C:\Users\user\Desktop\stck.png")

www.whosebug.com 替换为您的网站,并将 C:\Users\user\Desktop\stck.png 替换为您的输出图像路径。

用法:urltoimage(website, path)

Ps。谁看懂这段代码,你就知道它有多蠢:) ..... 但它有效!

Public Sub ShowPrintDialog()
    Dim print = Xpcom.QueryInterface(Of nsIWebBrowserPrint)(Me.Window.DomWindow)
    Try
        print.Print(print.GetGlobalPrintSettingsAttribute, Nothing)
    Catch ex As Exception
    End Try
End Sub

这是在 geckofx 中打印的代码。

另一方面,打印预览是另外一回事,到目前为止我无法让它工作。

https://bitbucket.org/geckofx/geckofx-22.0/issues/10/print-preview-now-in-detail

https://bitbucket.org/geckofx/geckofx-18.0/issues/54/print-preview