1

In my windows form I have a web page and I want to print the contents of it , either way, direct print to printer or saving bitmap and print later.

So far I have managed to get the code from internet and start saving .png files, but they all blank.

here is the code:

Public Function GenerateScreenshot(ByVal url As String) As Bitmap
    ' This method gets a screenshot of the webpage
    ' rendered at its full size (height and width)
    Return GenerateScreenshot(url, -1, -1)
End Function
Public Function GenerateScreenshot(ByVal url As String, ByVal width As Integer, ByVal height As Integer) As Bitmap
    ' Load the webpage into a WebBrowser control
    Dim wb As New WebBrowser()
    wb.ScrollBarsEnabled = False
    wb.ScriptErrorsSuppressed = True
    wb.Navigate(browse.Url)
    While wb.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While


    ' Set the size of the WebBrowser control
    wb.Width = width
    wb.Height = height

    If width = -1 Then
        ' Take Screenshot of the web pages full width
        wb.Width = wb.Document.Body.ScrollRectangle.Width
    End If

    If height = -1 Then
        ' Take Screenshot of the web pages full height
        wb.Height = wb.Document.Body.ScrollRectangle.Height
    End If

    ' Get a Bitmap representation of the webpage as it's rendered in the WebBrowser control
    Dim bitmap As New Bitmap(wb.Width, wb.Height)
    Using G As Drawing.Graphics = Drawing.Graphics.FromImage(bitmap)
        G.Clear(Color.Black)
    End Using
    wb.DrawToBitmap(bitmap, New Rectangle(0, 0, wb.Width, wb.Height))
    wb.Dispose()       
    Return bitmap
End Function

and here using the code:

 Try
        Dim thumbnail As Bitmap = GenerateScreenshot(browse.Url.ToString())
        thumbnail = GenerateScreenshot(browse.Url.ToString())

        Dim fn As String = Path.Combine(Path.GetTempPath(), "1_.png")
    thumbnail.Save(fn, System.Drawing.Imaging.ImageFormat.Png)
    Catch ex As Exception
    End Try

kindly help me out and suggest if i can print directly after saving to bitmap

DareDevil
  • 5,249
  • 6
  • 50
  • 88
  • What's the problem with `wb.Print()`? – Reza Aghaei Sep 01 '16 at 14:58
  • Basically, I'm loading URL. Contents into 'gecko' web browser control and it has no print functionality, So far that I need to capture the contents into background web browser control and print. – DareDevil Sep 01 '16 at 17:10
  • You can simply load the same URL in a `WebBrowserControl` and call its print method. Take a look at [this post](http://stackoverflow.com/questions/35933469/how-to-plot-svg-file-with-net). – Reza Aghaei Sep 01 '16 at 17:24
  • But tell me one thing, if the website which is going to be loaded via credentials, and they have check on it, the session variable, then this above method won't work.. Any help in that regard? – DareDevil Sep 01 '16 at 17:27
  • I don't have not any idea about *gecko*. If session and login is important you should rely on *gecko*'s features. But if session and login is not important the linked post will help you. – Reza Aghaei Sep 01 '16 at 17:31
  • @DareDevil, try [this approach](http://stackoverflow.com/a/21828265/1768303) which uses `OleDraw`. – noseratio Sep 05 '16 at 10:32

0 Answers0