Click here to Skip to main content
13,143,374 members (34,367 online)
Click here to Skip to main content
Add your own
alternative version

Stats

24.3K views
11 bookmarked
Posted 22 Apr 2014

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

, 1 Jan 2015
Rate this:
Please Sign up or sign in to vote.
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:

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.

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"

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

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:

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)

Share

About the Author

No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionit exceeds the try statement and execute the catch Pin
honar.cs22-Mar-15 5:42
memberhonar.cs22-Mar-15 5:42 
AnswerRe: it exceeds the try statement and execute the catch Pin
Benjamin Lifshitz22-Mar-15 9:28
memberBenjamin Lifshitz22-Mar-15 9:28 
Questioni mean it always execute the exception and give me the exception message Pin
honar.cs22-Mar-15 4:40
memberhonar.cs22-Mar-15 4:40 
AnswerRe: i mean it always execute the exception and give me the exception message Pin
Benjamin Lifshitz22-Mar-15 5:10
memberBenjamin Lifshitz22-Mar-15 5:10 
Questionalways a bug message? Pin
honar.cs21-Mar-15 23:06
memberhonar.cs21-Mar-15 23:06 
AnswerRe: always a bug message? Pin
Benjamin Lifshiz22-Mar-15 4:32
memberBenjamin Lifshiz22-Mar-15 4:32 
GeneralMy vote of 5 Pin
JohnPap_CP3-Jan-15 6:46
memberJohnPap_CP3-Jan-15 6:46 
AnswerNot an article Pin
Christian Amado22-Apr-14 7:56
professionalChristian Amado22-Apr-14 7:56 
GeneralRe: Not an article Pin
Benjamin Lifshitz22-Apr-14 8:13
memberBenjamin Lifshitz22-Apr-14 8:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 1 Jan 2015
Article Copyright 2014 by Benjamin Lifshitz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid