Monthly Archives: September 2010

Windows Forms Graphics: How to Merge and Print Images

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.

    Return MergedImage


  End Function

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)


  End Sub

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)

    e.Graphics.DrawImage(PictureBox3.Image, Rect)


  End Sub

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


  End Sub

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. 

DentBetty Generates Car Repair Estimates from Damage Photos [Cars]

If you’ve got a ding, dent, or busted up fender you want to get repair estimates for, web site DentBetty gets you repair estimates based on damage photos you submit. More »

The Best Photography Apps for Your iPhone [IPhone]

With the quality of cellphone cameras approximating that of yesterday’s point-and-shoots, you can take some amazing photographs on your iPhone. It gets even better with the right apps apps. Here are our favorite photography apps for your iPhone. More »

Ecosquid Is Like Expedia For Gadget Selling, Recycling [Gadget Recycling]

Services like Gazelle and ecoNew are great, because you can sell them your old, unwanted gadgets for a decent price, and they’ll be recycled, which is better than just throwing them in the trash. Ecosquid makes this process easy. More »

Fix Bad Video Interlacing in Digital Movies [Video]

Bad video interlacing leaves distracting horizontal lines across your screen during video playback (especially when there’s a lot of on-screen motion). Tech blog Tested details how to fix video interlacing on the fly. More »

How to Get Cheap or Free In-Flight Wi-Fi [Air Travel]

Want to stay connected above 10,000 feet but don’t want to pay the premium? Here’s how to get in-flight Wi-Fi at a discount or even completely free. More »

Bubble Cursor

The Bubble Cursor demo is a demo implementation of a cursor with a «nearest target» system that may help users aim their cursors at on-screen elements without having to hit them precisely. The «active» element is shown visually, using a bubble around the cursor.

Read more about it at Jochen Wolters’ blog, where he writes about all kinds of things related to usability, from airline reward miles to toilet paper.

If you require a short url to link to this article, please use

KeepItWith.Me Offers Dead Simple URL Sharing Between Devices [Video]

If you’d like a dead simple way to shuttle a URL from your desktop to your iPad, your smartphone to your desktop, or from any browser to any other browser, KeepItWith.Me uses a one-click system to share links between devices and browsers. More »

Make and Host Your Own Customized, Personal Landing Page in a Few Minutes [Video]

Personal landing like are really popular right now, but we prefer more complete control over what Google has to say about us. Here’s a Lifehacker-made, easily customizable, open-source solution that you can place on any server and have running in minutes. More »

App Marketing 101: Tips for Getting Reviewed

One of the most commonly asked questions is how to get your app reviewed by the various iPhone blog sites around the Internet. While there’s no guaranteed method,‘s co-founder Ryan Johnson has a few pointers that may help:

  • Understand the blog’s preferred method of communication. Each blog has a different process: Some have an email address for submissions; others use a web form. Tracking down the email addresses of all the site’s contributors doesn’t mean an out-of-the-blue email will be welcome, so it’s crucial to research the submission process before sending the app.
  • Don’t force reviewers to waste time doing research. Simply submitting your app with no explanation about the product is a surefire way to not get reviewed. At the very least, provide your company name, along with the app name, its category, an overview of its highlights and a direct link.
  • Spice it up with some media. You’ll hardly ever see an app reviewed without an image or video to catch the reader’s eye. Make it easy for reviewers and include your screenshots, videos, promo art, icons, etc. with your submission. Do not, however, send them as attached files unless specifically requested. Host your images on Photobucket or one of the various other image-hosting sites and send the link to the image. The same goes for videos.
  • Provide a Promo Code. If there’s a strong possibility a certain site will review your app, and your app is paid, go ahead and provide a promo code (make sure the writer is in the U.S.; codes don’t work in other app stores).
  • Stick to the theme of the site. Some sites, like for instance, have very specific focuses. ( is specifically for games.) It’s a waste of your time, as well as the bloggers’ time, to spend time with submissions that do not fit with the site in question.

The best thing you can do, of course, is build a relationship with these people over time. You can meet many of them at various events around the country including MacWorld, WWDC and other conferences. If their site has a forum, engage them in conversation so that when it comes time to talk about your app, they know you. Finally, if you don’t have time for that, find a PR agent that does.

This is the final post in a three-part series. To learn more about marketing iPhone apps, read part 1 and part 2, which were posted earlier this week. For an in-depth analysis, download the GigaOM Pro report, “How to Market Your iPhone App: A Developer’s Guide” (subscription required).

Image Source: flickr user Yutaka Tsutano.

WP Like Button Plugin by Free WordPress Templates