Print picture box image to fit page

SeanPress 206 Reputation points
2023-01-12T00:14:23.45+00:00

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

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-01-12T08:17:16.96+00:00

    Hi @SeanPress , You can control the size of the image drawn on paper via the PrintPage event.

        Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
            Dim i As Image = Image.FromFile("C:\Users\Administrator\Desktop\Cat.jfif")
            e.Graphics.DrawImage(i, e.MarginBounds)
        End Sub
    

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.