In my application I have a number of window forms that need to printed.
The end users all have different size screens the lowest being 1366 x 768.
I tried using PrintForm but the program only prints what is on the screen so the forms have to be small and therefore would only fill part of an A4 sheet.
Therefore I want to be able to capture the form and scale it up to A4 for printing.
I copied some code from the internet and modified it for my use but it does not seem to scale up the form although it does print it.
Has any one any ideas how I can do this.
Please be aware I am a total amateur at this. Producing the software for the Seniors Section of a golf club.
What I have tried:
The code am using is as follows
Public Class TeamSelect12
'Define your print object
Private PrintDoc1 As PrintDocument = New PrintDocument
Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog
Private PageSetupDialog1 As PageSetupDialog = New PageSetupDialog
'Print Button Code
Private Sub PrtTSBut_Click(sender As System.Object, e As System.EventArgs) Handles PrtTSBut.Click
PDFTeamBut.Visible = False 'Hides button
PrtTSBut.Visible = False 'Hides button
Me.Refresh()
With PageSetupDialog1
'Assign the document to use
.Document = PrintDoc1
'Enable printer button
.AllowPrinter = True
.EnableMetric = True
' Initialize the dialog's PrinterSettings property to hold user
' defined page settings.
.PageSettings = New System.Drawing.Printing.PageSettings
' Initialize dialog's PrinterSettings property to hold user
' set printer settings.
.PrinterSettings = New System.Drawing.Printing.PrinterSettings
End With
PrintPreviewDialog1.Document = PrintDoc1
AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage
Dim margins As New Margins(20, 20, 20, 40)
PrintDoc1.DefaultPageSettings.Margins = margins
PrintPreviewDialog1.ShowDialog()
'Replace buttons back on form
PrtTSBut.Visible = True
PDFTeamBut.Visible = True
'Hide form
Me.Hide()
InputForm.Show() ' Return to input form
End Sub
'Print Handler And Graphics print method With scaling
Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs)
PrintToGraphics(e.Graphics, e.MarginBounds)
End Sub
Public Sub PrintToGraphics(Graphics As Graphics, bounds As Rectangle)
'Print the control's view to a Graphics object.
'<param name="graphics">Graphics object to draw on.</param>
'<param name="bounds">Rectangle to print in.</param>
'Draw the control and contents to a bitmap
Dim Bitmap As Bitmap = New Bitmap(Me.Width, Me.Height)
Me.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Bitmap.Height))
'Assign Print Bounds to target rectangle
Dim PrtWidth, PrtHeight, PrtLeft, PrtTop As Integer
PrtWidth = bounds.Width
PrtHeight = bounds.Height
PrtLeft = bounds.Left
PrtTop = bounds.Top
Dim target As Rectangle = New Rectangle(PrtLeft, PrtTop, PrtWidth, PrtHeight)
'Scale bitmap to fit target
Dim xScale As Double = Bitmap.Width / PrtWidth
Dim yScale As Double = Bitmap.Height / PrtHeight
If xScale < yScale Then
target.Width = Int(xScale * target.Width / yScale)
Else
target.Height = Int(yScale * target.Height / xScale)
End If
'Draw the bitmap
Graphics.PageUnit = GraphicsUnit.Display
Graphics.DrawImage(Bitmap, target)
End Sub