Printing Images Quickly and Efficiently to a A4 Page [VB.NET]

This tip shows you a quick technique for printing images, when the image is being printed with utilization of the biggest area available of the A4 page. This code supports almost any image, even special cases such as panoramas.

These are the operations the code does:

  1. Gets the aspect ratio of the original image (width/height).
  2. If the image width is larger than the image height, the app rotates it.
  3. Converts the original image to an image optimized for A4 paper size. It does not stretch the image, and keeps the original aspect ratio, by changing the image width to A4 paper width and calculating the height byte aspect ratio.
  4. If the edited image height is larger than the A4 height, the image is being resized again to a smaller size.
  5. Does again operation number 4.
  6. Draws the resized image to a bitmap, and prints it by the printdocument component.

  1. We have an image. Size: 2816 X 2112 pixels.
  2. Because the image width is larger than the image height, the image is been rotated.
  3. The A4 height: 1100 Pixels.
  4. The image aspect ratio (width / height): 0.75
  5. The new image width: 0.75 * 1050 = 825 pixels.
  6. The new image, with the new width and height, is being drawn and sent to the printer.
  7. The result (previewed as xps file):

First, we need to declare our components. The components we need for this sample are two PictureBoxes (Picturebox1 and Picturebox2), and one PrintDocument item (prntdoc).

  • Picturebox1 – will have the original image.
  • Picturebox2 – will be used when we fit our image to A4 size. Its parameter visible should be false (because it is being used “behind the scenes” only).

Preparation:

Try
PictureBox2.image = PictureBox1.image

First, we will check if the image width is larger than its height. If so, we will rotate the image, for it to catch the biggest area available of the A4 page.

If PictureBox2.Image.Width > PictureBox2.Image.Height Then
PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
End If

Then, we will put the text “1100” (the A4 height) to TextBox1.

TextBox1.text = "1100"

Now we will declare the height and width variables.

h = PictureBox2.Image.Size.Height
w = PictureBox2.Image.Size.Width
Dim Height As Double = Convert.ToDouble(TextBox1.Text)
Dim width As Double
Dim aspectRatio As Double

Now we get the aspect ratio of the image, and multiply it with 1100 (the A4 height).

 aspectRatio = PictureBox2.Image.Width / PictureBox2.Image.Height
 width = Convert.ToDouble(TextBox1.Text * aspectRatio)

There may be a case that the image width is larger than the A4 width (891 pixels). Then, we will resize the image to a smaller size:

If width > 891 Then
                PictureBox2.Image = PictureBox1.Image
                PictureBox1.Visible = False
                If PictureBox2.Image.Width > PictureBox2.Image.Height Then
                    PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
                End If
                TextBox1.Text = "900"

                h = PictureBox2.Image.Size.Height
                w = PictureBox2.Image.Size.Width

                Height = Convert.ToDouble(TextBox1.Text)

                aspectRatio = PictureBox2.Image.Width / PictureBox2.Image.Height
                width = Convert.ToDouble(TextBox1.Text * aspectRatio)
                If width > 891 Then
                    PictureBox2.Image = PictureBox1.Image
                    PictureBox1.Visible = False
                    If PictureBox2.Image.Width > PictureBox2.Image.Height Then
                        PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
                    End If
                    TextBox1.Text = "700"

                    Height = Convert.ToDouble(TextBox1.Text)
                    width = Convert.ToDouble(TextBox1.Text * aspectRatio)
                Else

After all that, the code draws the image and sends it to the printer.

  Dim wi As Integer
                    wi = Convert.ToInt32(width)
                    Dim hi As Integer
                    hi = Convert.ToInt32(Height)
                    Dim New_Bitmap As New Bitmap(PictureBox2.Image, wi, hi)
                    PictureBox2.Image = New_Bitmap
                    w = width
                End If
            Else
                Dim wi As Integer
                wi = Convert.ToInt32(width)
                Dim hi As Integer
                hi = Convert.ToInt32(Height)
                Dim New_Bitmap As New Bitmap(PictureBox2.Image, wi, hi)
                PictureBox2.Image = New_Bitmap
                w = width
            End If
            '    End If
            prntdoc.Print()
        Catch ex As Exception
           msgbox("bug.")
        End Try

Final stage: When the PrintDocument item is being activated, it sends to the printer the final bitmap (with no margins beacuse we use the full size of A4 page).

Private Sub prntdoc_PrintPage1(sender As Object, e As PrintPageEventArgs) Handles prntdoc.PrintPage
            e.PageSettings.Margins = New Margins(0, 0, 0, 0)
            e.Graphics.DrawImage(PictureBox2.Image, 0, 0)
End Sub

All the code combined:

Try 
 
PictureBox2.image = PictureBox1.image 
If PictureBox2.Image.Width > PictureBox2.Image.Height Then
PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone) 
End If   
TextBox1.text = "1100"
Dim Height As Double = Convert.ToDouble(TextBox1.Text)
Dim width As Double

Dim aspectRatio As Double
aspectRatio = PictureBox2.Image.Width / PictureBox2.Image.Height
 width = Convert.ToDouble(TextBox1.Text * aspectRatio)  
If width > 891 Then
                PictureBox2.Image = PictureBox1.Image
                PictureBox1.Visible = False
                If PictureBox2.Image.Width > PictureBox2.Image.Height Then
                    PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
                End If
                TextBox1.Text = "900"

                h = PictureBox2.Image.Size.Height
                w = PictureBox2.Image.Size.Width

                Height = Convert.ToDouble(TextBox1.Text)

                aspectRatio = PictureBox2.Image.Width / PictureBox2.Image.Height
                width = Convert.ToDouble(TextBox1.Text * aspectRatio)
                If width > 891 Then
                    PictureBox2.Image = PictureBox1.Image
                    PictureBox1.Visible = False
                    If PictureBox2.Image.Width > PictureBox2.Image.Height Then
                        PictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
                    End If
                    TextBox1.Text = "700"

                    Height = Convert.ToDouble(TextBox1.Text)
                    width = Convert.ToDouble(TextBox1.Text * aspectRatio)
                Else
                    Dim wi As Integer
                    wi = Convert.ToInt32(width)
                    Dim hi As Integer
                    hi = Convert.ToInt32(Height)
                    Dim New_Bitmap As New Bitmap(PictureBox2.Image, wi, hi)
                    PictureBox2.Image = New_Bitmap
                    w = width
                End If
            Else
                Dim wi As Integer
                wi = Convert.ToInt32(width)
                Dim hi As Integer
                hi = Convert.ToInt32(Height)
                Dim New_Bitmap As New Bitmap(PictureBox2.Image, wi, hi)
                PictureBox2.Image = New_Bitmap
                w = width
            End If
            '    End If
            prntdoc.Print()
        Catch ex As Exception
           msgbox("bug.")
        End Try

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.