XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

AugSeptember 2006Oct
SMTWTFS
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

Archives

Topics

Ramblings

VB.NET

Thursday, September 21, 2006 #

   Mike McIntyre's recent blog item about breaking large images into chunks in order to print them prompts me to post up an approach to the (kind of) opposite scenario which by coincidence I was working on yesterday.   I had two images on a Windows Form, each in its own PictureBox control and I wanted to merge the two images, placing them side by side and printing them out on the printer.

  It turned out to be pretty easy.

  This function takes the two images as parameters, merges them and returns the merged image:


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.

   If Pic1.Height >= pic2.Height Then

     High = Pic1.Height

   Else

     High = pic2.Height

   End If

 

' 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 a black line round the outside (optional, but sometimes looks better when printed)

gr.DrawRectangle(Pens.Black, 0, 0, Wide - 1, High - 1)

' 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

gr.Dispose()

 

' You now have an Image named MergedImage which you can print.

Return MergedImage

 

End Function


     The next code block is the PrintPage event of a PrintDocument component that I dragged on to the form (in this example named "PrintImageDocument").   It uses the function above to create the image.  It then sets the start position and size of the image for the printing before finally drawing the image on the Graphics object.


    Private Sub PrintImageDocument_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintImageDocument.PrintPage

        '  First create the image using the function prepared above:

        Dim ImageToPrint As Image = MergeImages(picValues.Image, picGraph.Image)

 

        '  Now Print it - change "20, 20" to whatever X and Y position values

        '  you want as your start point for printing on the page

        Dim R As New Rectangle(20, 20, ImageToPrint.Width, ImageToPrint.Height)

        e.Graphics.DrawImage(ImageToPrint, R)

    End Sub


    And finally, in this example I used a button to fire the Print event of the PrintDocument component, which (of course) causes the image to be sent to the printer for hard copy printing.

 


      Private Sub ButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonPrint.Click

        Me.PrintImageDocument.Print()

    End Sub


 

posted @ 5:33 AM

  I see that a couple of free Power Packs have just been released into the community which will be of particular interest to VB6 Upgraders who haven't yet managed to totally cut the Classic VB umbilical chord .

  Details from the download page are:

Microsoft Interop Forms Toolkit 1.0
This toolkit helps you bring the power of .NET to your existing Visual Basic 6 applications, by allowing them to display .NET WinForms from within the same application. Instead of upgrading the entire code base, these applications can now be extended one form at a time. The goal is a phased upgrade, with production releases at the end of each iteration containing both Visual Basic 6 and Visual Basic .NET forms running in the same Visual Basic 6 .exe process.   

Microsoft PrintForm Component 1.0
The PrintForm component is designed to bring back the ability to easily print a Windows Form. With this the new PrintForm component, you can once again lay out the Windows Form exactly as you want it and allow your users to print the form as a quick report.   

 

 

posted @ 4:52 AM