vb.net - 打印组框及其内容并将其放入 A4 尺寸打印
问题描述
我正在打印组框,但打印不正确。如何打印并适合 A4 尺寸的纸张。这是我的代码
Private Sub printbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printbtn.Click
PrintDialog1.Document = PrintDocument1 'PrintDialog associate with PrintDocument.
If PrintDialog1.ShowDialog() = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
Dim bmp As New Bitmap(Me.GroupBox1.Width, Me.GroupBox1.Height)
Me.GroupBox1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.GroupBox1.Width, Me.GroupBox1.Height))
e.Graphics.DrawImage(DirectCast(bmp, Image), x, y)
End Sub
并获得像这样的输出点击
解决方案
这里的代码:
Dim WithEvents PrintDocument1 As Printing.PrintDocument = New Printing.PrintDocument
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintDocument1.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
Dim bmp As New Bitmap(Me.GroupBox1.Width, Me.GroupBox1.Height)
Me.GroupBox1.DrawToBitmap(bmp, New Rectangle(0, 0, Me.GroupBox1.Width, Me.GroupBox1.Height))
'Get the A4 size
Dim limitX As Integer = e.PageBounds.Width
'use this to reduce your screenshot for the A4 format page
Dim reduceMe As Double = limitX / Me.GroupBox1.Width
Dim reducedBmp As New Bitmap(bmp, CInt(Me.GroupBox1.Width * reduceMe), CInt(Me.GroupBox1.Height * reduceMe))
e.Graphics.DrawImage(reducedBmp, x, y)
End Sub
推荐阅读
- apache-spark - How to get Mesos Agents Framework Executor Memory
- c# - 插件互操作访问速度比 Windows 应用程序快 15-20 倍
- linux - 使用空字节作为分隔符将每个参数与参数的位置编号连接起来?
- javascript - Leaflet 自定义图层控件
- java - Servlet/JSF:将参数从过滤器传递到稍后调用的 bean 方法
- int - 将文字标记为 32 位
- azure - 使用自托管 Azure Service Fabric 调用 Web API 时出错
- excel - Excel公式:如果当前时间晚于那么
- c# - C#:如何在进行 HttpWebrequest 之前获得 SSL 握手响应?
- ajax - 带有 AJAX 请求的 django-wkhtmltopdf 将获得 403 Forbidden