This example shows how to take two images, merge them side by side, then print them. I’ve chosen to put them side by side, but you can easily change the logic so that they’re placed one on top of the other.
To shows the steps, I’ve used three PictureBoxes.
Two of them have images assigned. The third PictureBox will be used to display the merged image.
It’s important to realize that there’s no need to have any of the images on display for the code to work. But it does make it a lot easier to see the steps.
The Merge function
Here’s a function that combines the two images. Essentially it creates a canvas large enough to take the size of both incoming images and then pastes one to the left side and one to the right. OK, so it’s not really a canvas, but I think the analogy helps visualise it.
Public Function MergeImages(ByVal Pic1 As Image, ByVal pic2 As Image) As Image
Dim MergedImage As Image ' This will be the finished merged image
Dim Wide, High As Integer ' Size of merged image
' Calculate Width and Height needed for composite image
' First, the Width:
Wide = Pic1.Width + pic2.Width
' Height: Ensure that the new image is high enough for both images
' that we plan to place inside it.
High = If(Pic1.Height >= pic2.Height, Pic1.Height, pic2.Height)
' Create an empty Bitmap the correct size to hold both images side by side
Dim bm As New Bitmap(Wide, High)
' Get the Graphics object for this bitmap
Dim gr As Graphics = Graphics.FromImage(bm)
' Draw the first source image at left side of new image
gr.DrawImage(Pic1, 0, 0)
' Draw second source image, offset to the right edge of first source image
gr.DrawImage(pic2, Pic1.Width, 0)
' Assign the merged bitmap you have just created as the image
' you are going to return for printing
MergedImage = bm
' Finished with the Graphics object – dispose of it
' You now have an Image named MergedImage which you can return.
I don’t think there’s much explanation beyond the comments needed. Obviously, two image objects are passed in and one is returned at the end of the process.
The Merge Button
All this button does is call the MergeImages function and passes in the images from the first two PictureBoxes.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox3.Image = MergeImages(PictureBox1.Image, PictureBox2.Image)
At this stage, the form will look like this:
The PrintDocument control
I dragged a PrintDocument control onto the form and it sits itself in the component tray. The PrintDocument has a PrintPage event which does what you’d expect. All that’s needed here is to give it the details of exactly what you want to print and how you want it laid out. This code does that job:
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim Rect As New Rectangle(20, 20, PictureBox3.Width, PictureBox3.Height)
The first line creates a Rectangle that’s large enough to hold the finished image that will be in PictureBox3 after the Merge button has been used. (You’ll see from the screenshot that there’s a lot of unwanted whitespace in PictureBox3, and this is dealt with by the Print button that I’ll come to next.) The values of 20,20 in that first code line fix the start point on the printed page for the top left corner of the image print. Obviously, this can be changed.
The second line uses the DrawImage method of the System.Drawing.Image class. It takes the image from PictureBox3 and draws it within a rectangular area described by the variable named Rect.
The Print Button
Here’s the code in the click event of the Print button:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Resize PictureBox3 to take its merged image exactly
PictureBox3.Width = PictureBox3.Image.Width + 2
PictureBox3.Height = PictureBox3.Image.Height + 2
' Print the image
The Print button does a bit of housework first. It removes any unneeded area of the PictureBox. It doesn’t particularly matter in this example because the background is white, but if there was a coloured background that would be part of the printout, it would probably look better without any large areas of image-less color.