在我的 Windows 窗体中,我有一个网页,我想打印它的内容,无论哪种方式,直接打印到打印机或保存位图并稍后打印。
到目前为止,我已经设法从互联网上获取代码并开始保存.png
文件,但它们都是空白的。
这是代码:
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
在这里使用代码:
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
请帮助我并建议我是否可以在保存到位图后直接打印