vb.netprintingbitmapstretch

print Bitmap to A4 size without stretch


enter image description here

Public Class Form1

    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim margX As Single = e.PageSettings.HardMarginX
        Dim margY As Single = e.PageSettings.HardMarginY
        Dim adjustToA4 As New Rectangle(e.PageBounds.Left - margX, e.PageBounds.Top - margY, e.PageBounds.Width, e.PageBounds.Height)


        Dim dm As New Bitmap(Panel2.Width, Panel2.Height)
        Panel2.DrawToBitmap(dm, New Rectangle(0, 0, Panel2.Width, Panel2.Height))

        e.Graphics.DrawImage(dm, adjustToA4)

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()

    End Sub

End Class

This coding allows the bitmap to print to A4 size... but the bitmap is not in its original form in A4 size. therefore, what is the coding that can enlarge or reduce the bitmap to A4 size without affecting the shape of the bitmap.. thank you...

"translation"....

enlarge or reduce bitmap to A4 size without stretch.... VB 2022


Solution

  • Public Class Form1
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
        PrintPreviewDialog1.Document = PrintDocument1
        PrintPreviewDialog1.ShowDialog()
    
        PrintPreviewDialog1.Document = PrintDocument2
        PrintPreviewDialog1.ShowDialog()
    
    End Sub
    
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    
        Dim margX As Single = e.PageSettings.HardMarginX
        Dim margY As Single = e.PageSettings.HardMarginY
        Dim adjustToA4 As New Rectangle(e.PageBounds.Left - margX, e.PageBounds.Top - margY, e.PageBounds.Width, e.PageBounds.Height)
    
        Dim dm As New Bitmap(Panel2.Width, Panel2.Height)
        Panel2.DrawToBitmap(dm, New Rectangle(0, 0, Panel2.Width, Panel2.Height))
    
        e.Graphics.DrawImage(dm, adjustToA4)
    
    End Sub
    
    Private Sub PrintDocument2_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument2.PrintPage
        Dim adjustToA4 As New Rectangle(25, 30, e.PageBounds.Width - 60, e.PageBounds.Height - 50)
    
        Dim dm As New Bitmap(Panel1.Width, Panel1.Height)
        Panel1.DrawToBitmap(dm, New Rectangle(0, 0, Panel1.Width, Panel1.Height))
    
        e.Graphics.DrawImage(dm, adjustToA4)
    End Sub
    

    End Class

    Form1 PrintPreview2 PrintPreview1