【问题标题】:Pass Windows.Forms.WebBrowser Contents into PrintDocument将 Windows.Forms.WebBrowser 内容传递到 PrintDocument
【发布时间】:2014-04-25 11:45:04
【问题描述】:

Visual Studio 2010,Visual Basic.NET

我在表单上有一个 WebBrowser Control(wbImages),我需要允许用户打印该 WebBrowser Control 的内容。如果我使用 wbImages.Print(),它将打印文档,但只使用默认打印机。所以我想打开打印对话框以允许用户更改打印机。不幸的是,我不知道如何将 wbImages 的内容转换为 PrintDocument。

这是我目前拥有的。

Private Sub PrintToolStripMenuItem_Click(sender As System.Object, _ 
        e As System.EventArgs) Handles PrintToolStripMenuItem.Click

    'I need to get the wbImages into pdocImages
    'pdocImages = ConvertPrintDoc(wbImages)

    pdImages.Document = pdocImages
    pdImages.PrinterSettings = pdocImages.PrinterSettings
    pdImages.AllowSomePages = True
    If pdImages.ShowDialog = DialogResult.OK Then
        pdocImages.PrinterSettings = pdImages.PrinterSettings

        If imageUrl.IndexOf(".jpg") <> -1 Or imageUrl.IndexOf(".gif") <> -1 Then
            MessageBox.Show("Image has been sent to the printer.")
            wbImages.Print()
        Else
            MessageBox.Show("Table has been sent to the printer.")
            wbImages.Print()
        End If

    End If

End Sub

【问题讨论】:

  • 不可能。您必须使用WebBrowser.Print() 方法。
  • 所以用户在打印网页时永远无法更换打印机?
  • 使用 WebBrowser.ShowPrintDialog()
  • 哇,伙计,我很高兴你发布了这个!!!!像魅力一样工作,非常感谢!!!

标签: vb.net visual-studio-2010 .net-4.0


【解决方案1】:

该方法会将浏览器中当前可见的内容绘制成位图,然后打印出来:

Private Sub PrintToolStripMenuItem_Click(sender As System.Object, _ e As System.EventArgs)  Handles PrintToolStripMenuItem.Click
    PrintDocument1.Print()
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    e.Graphics.DrawImage(TakeSreenShot(WebBrowser1), 0, 0)
End Sub

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

显然它只会打印当前可见的内容,这是一个缺点,但我不知道有任何其他方法可以做到这一点。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多