在Vb.net中打印GeckoWebBrowser控件

时间:2016-07-25 11:57:52

标签: vb.net webbrowser-control gecko

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

我找不到和帮助材料所以我试图抓住页面的截图和打印,但它在第一页之后就错过了。 我正在使用MicrosoftPowerPack库。 下面是我试图打印页面的代码。

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

3 个答案:

答案 0 :(得分:1)

此代码将页面输出到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.stackoverflow.com", "C:\Users\user\Desktop\stck.png")

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

用法:urltoimage(website, path)

聚苯乙烯。无论谁理解这段代码,你都知道它是多么愚蠢:) .....但它确实有效!

答案 1 :(得分:1)

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

答案 2 :(得分:0)

屏幕截图功能是:

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替换为您的图片路径。

然后你可以打印图像。