在Vb.net中打印GeckoWebBrowser控件

在Vb.net中打印GeckoWebBrowser控件,vb.net,webbrowser-control,gecko,Vb.net,Webbrowser Control,Gecko,我正在使用vb.net winform中的GeckoWebBrowser控件,希望将页面内容直接打印到默认打印机 我找不到有帮助的材料,所以我试着截图和打印页面,但第一页之后就漏掉了。 我正在使用MicrosoftPowerPack库。 下面是我试图打印页面的代码 Dim settings As New System.Drawing.Printing.PrinterSettings PrintForm1.PrinterSettings = settings setting

我正在使用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)

屏幕截图功能是:

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)

用GeckoWebBroswer替换
WebBroswer1
,用图像路径替换
C:\Users\user1\Desktop\test.png


然后您可以打印图像。

屏幕截图功能是:

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)

用GeckoWebBroswer替换
WebBroswer1
,用图像路径替换
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.stackoverflow.com”,“C:\Users\user\Desktop\stck.png”)

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

用法:
urltoimage(网站、路径)


不管谁理解这个代码,你都知道它有多蠢:)。。。。。但它是有效的

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

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
它的速度很慢,因为它可以保存非常高分辨率的图像。但这取决于你的上网速度

将其放在代码的最上面:

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(网站、路径)

不管谁理解这个代码,你都知道它有多蠢:)。。。。。但它是有效的

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打印的代码

另一方面,打印预览是另一回事,到目前为止,我还不能让它工作

这是用geckofx打印的代码

另一方面,打印预览是另一回事,到目前为止,我还不能让它工作



您可以拍摄broswer的屏幕截图,然后打印出来。如果你想要截图代码,我可以给你。如果你能分享,我会很高兴的。。我做了很多事情。但在瓦伊尼写下它作为一个答案。但潘告诉你。什么。。这类实验是在两天的时间内完成的。它不会打印整个页面,而是可见页面。你可以拍摄一张broswer的截图,然后打印出来。如果你想要截图代码,我可以给你。如果你能分享,我会很高兴的。。我做了很多事情。但在瓦伊尼写下它作为一个答案。但潘告诉你。什么。。这类实验是在两天的探索中完成的。它不会打印整个页面,而是可见页面。如果它不起作用,请告诉我你正在使用的gecko版本。但是没有,事实上我正在忙于其他任务。。因此,明天将进行一次尝试。显然,代码看起来不错,我确实尝试过,但不幸的是,由于这里的网络安全,无法检索结果,但感谢您为我提供了解决问题的新方向。如果它不起作用,请告诉我您正在使用的gecko版本。但是,事实上,我正在忙于其他任务。。因此,明天将进行一次尝试。显然,代码看起来不错,我确实尝试过,但不幸的是,由于这里的网络安全,无法检索结果,但感谢您为我提供了解决问题的新方向。