Hi,
I have a Visual Basic form and have a PictureBox on it with a jpg image. I have PrintPreview & Print button, when I try to print preview or print the image it is too big for the preview or paper. I have copied my code below, can anyone tell me where my mistakes are or how to get the print preview to show the whole page & print the PictureBox image to fit whichever page size I select on my printer?
Public Class Form12
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.PrintPreviewControl.Zoom = 1
PrintPreviewDialog1.ShowDialog(PictureBox1)
End Sub
Private Sub PrintDocument1_QueryPageSettings(ByVal sender As Object, ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles PrintDocument1.QueryPageSettings
Dim id As Integer
If id = 1 Then
e.PageSettings.Landscape = False
Else
e.PageSettings.Landscape = True
End If
End Sub
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.DrawImage(PictureBox1.Image, 0, 35)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
If PrintDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
End If
Dim Lmargin, Rmargin, Tmargin, Bmargin As Integer
With PrintDocument1.DefaultPageSettings.Margins
Lmargin = .Left
Rmargin = .Right
Tmargin = .Top
Bmargin = .Bottom
End With
' Calculate the dimensions of the printable area
Dim PrintWidth, PrintHeight As Integer
With PrintDocument1.DefaultPageSettings.PaperSize
PrintWidth = .Width - Lmargin - Rmargin
PrintHeight = .Height - Tmargin - Bmargin
End With
Dim PageWidth, PageHeight As Single
With PrintDocument1.DefaultPageSettings.PaperSize
PrintWidth = .Width - Lmargin - Rmargin
PrintHeight = .Height - Tmargin - Bmargin
PageWidth = .Width
PageHeight = .Height
End With
With PrintDocument1
.DocumentName = "Gas Lift Troubleshooting"
.PrinterSettings.Copies = 1
.Print() 'trigger the print event
End With
End Sub
End Class