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