Click here to Skip to main content
15,887,333 members
Articles / Multimedia / Image Processing
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
4.47/5 (6 votes)
1 Jan 2015Ms-PL2 min read 40.2K   11  
With this code, you can print images quickly and efficiently, using the largest part of the A4 page for your image.

Introduction

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.

How It Works

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.

The Printing Component at Work

  1. We have an image. Size: 2816 X 2112 pixels.
  2. Because the image width is larger than the image height, the image has 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):

Here Comes the Code

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:

VB.NET
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.

VB.NET
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.

VB.NET
TextBox1.text = "1100"

Now, we will declare the height and width variables.

VB.NET
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).

VB.NET
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:

VB.NET
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"

                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.

VB.NET
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
              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
          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 because we use the full size of A4 page).

VB.NET
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 is as follows:

VB.NET
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"

                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
                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
            End If
            '    End If
            prntdoc.Print()
        Catch ex As Exception
           msgbox("bug.")
        End Try   

About the Code

The code is a part of Bell Office by Bell Software.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --