【问题标题】:Print Group Box and its contents and fit it in A4 size print打印组框及其内容并将其放入 A4 尺寸打印
【发布时间】:2019-10-20 11:31:20
【问题描述】:

我正在打印组框,但打印不正确。 如何打印并适合 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

并得到这样的输出click

【问题讨论】:

标签: vb.net


【解决方案1】:

代码如下:

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

【讨论】:

    【解决方案2】:

    这个怎么样:

    Dim srcRect As Rectangle = GroupBox1.ClientRectangle
    Dim desRect As New Rectangle(e.MarginBounds.X, e.MarginBounds.Y, e.MarginBounds.Width, GroupBox1.Height)
    Dim bmp As New Bitmap(srcRect.Width, srcRect.Height)
    
    GroupBox1.DrawToBitmap(bmp, srcRect)
    
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality
    
    e.Graphics.DrawImage(bmp, desRect, srcRect, GraphicsUnit.Pixel)
    
    bmp.Dispose()
    

    祝你好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 2010-10-29
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 2012-09-27
      • 2015-05-23
      相关资源
      最近更新 更多