【问题标题】:how do i print my form?我如何打印我的表格?
【发布时间】:2015-03-26 02:10:27
【问题描述】:

当用户单击按钮时,我需要打印我的 VB.Net Windows.Form 的图像。有什么好的方法吗?

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您需要使用PrintForm 组件。请看How to: Print a Form by Using the PrintForm Component:

    PrintForm 组件使您能够 快速打印表格的图像 与屏幕上显示的完全一样 不使用 PrintDocument 零件。以下程序 展示如何将表格打印到打印机, 打印预览窗口,以及 封装的 PostScript 文件。

    【讨论】:

      【解决方案2】:

      如果要修改图像,以下代码将捕获位图并将其发送到打印机(在 Button1 单击时)

      Imports System.Drawing.Printing
      
      Public Class Form1
      
      Dim WithEvents mPrintDocument As New PrintDocument
      Dim mPrintBitMap As Bitmap
      
      
      Private Sub m_PrintDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles mPrintDocument.PrintPage
          ' Draw the image centered.
          Dim lWidth As Integer = e.MarginBounds.X + (e.MarginBounds.Width - mPrintBitMap.Width) \ 2
          Dim lHeight As Integer = e.MarginBounds.Y + (e.MarginBounds.Height - mPrintBitMap.Height) \ 2
          e.Graphics.DrawImage(mPrintBitMap, lWidth, lheight)
      
          ' There's only one page.
          e.HasMorePages = False
      End Sub
      
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          ' Copy the form's image into a bitmap.
          mPrintBitMap = New Bitmap(Me.Width, Me.Width)
          Dim lRect As System.Drawing.Rectangle
          lRect.Width = Me.Width
          lRect.Height = Me.Width
          Me.DrawToBitmap(mPrintBitMap, lRect)
      
      
          ' Make a PrintDocument and print.
          mPrintDocument = New PrintDocument
          mPrintDocument.Print()
      End Sub
      End Class
      

      【讨论】:

      • 这个不错,但是没有考虑纸张大小。
      【解决方案3】:

      如果您真的想要表单的快照,则必须模拟屏幕截图。这是一个sample,它会有所帮助。不过它会非常难看,因此您可能需要考虑使用 PDF 或报告生成器将其制作成报告。

      【讨论】:

      • 呵呵,不知道。酷,tnx。 :)
      猜你喜欢
      • 1970-01-01
      • 2014-03-02
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-15
      相关资源
      最近更新 更多