DareDevil DareDevil - 1 year ago 180
Vb.net Question

Printing GeckoWebBrowser Control in Vb.net

I am working with GeckoWebBrowser control in vb.net winform, I wan to print the page contents direct to default printer.

I couldn't find and helping material so I was trying to take the page's screenshot and print, but it misses right after first page.
I am using

Below is the code which I am trying to print the page.

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

Answer Source

This code outputs a page to a png file: (Althought its slow and it freezes your program while it's running. Try putting it to a background worker to avoid freezing)

It's slow because it saves very high resolution images. But it depends on your internet speed.

Put that on the very top of your code:

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

The sub is:

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)
    Dim postresponse As HttpWebResponse
    postresponse = DirectCast(postReq.GetResponse, HttpWebResponse)
    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
End Sub

Ex. urltoimage("www.stackoverflow.com", "C:\Users\user\Desktop\stck.png")

Replace www.stackoverflow.com with you website and C:\Users\user\Desktop\stck.png with you output image path.

Usage: urltoimage(website, path)

Ps. Whoever understands this code you know how dumb it is :) ..... But it works !

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download