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:
- Gets the aspect ratio of the original image (width/height).
- If the image width is larger than the image height, the app rotates it.
- 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.
- If the edited image height is larger than the A4 height, the image is being resized again to a smaller size.
- Does again operation number 4.
- Draws the resized image to a bitmap, and prints it by the
printdocument
component.
The Printing Component at Work
- We have an image. Size: 2816 X 2112 pixels.
- Because the image width is larger than the image height, the image has been rotated.
- The A4 height: 1100 pixels.
- The image aspect ratio (width / height): 0.75
- The new image width: 0.75 * 1050 = 825 pixels.
- The new image, with the new width and height, is being drawn and sent to the printer.
- 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
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
prntdoc.Print()
Catch ex As Exception
msgbox("bug.")
End Try
About the Code
The code is a part of Bell Office by Bell Software.