Printing Multiple Images (Visual Basic)

Take the following steps to add code that prints two images on one page. This code shows you how to control the size and position of images on the page.

1.

Start with the project that you created in Loading and Displaying an Image.

2.

image\btncmd.gif Select the CommandButton control; then add the control to your main form. Put the control at the top of the form to keep it away from the image.

3.

In the Properties box, change the CommandButton control's Caption property to Print Twice on One Page.

4.

Add the following code to the CommandButton control's Click procedure. In online help, you can use the Edit pull-down menu to copy the block of code.

 

Notice that LEAD measures the printing surface in pixels, while Visual Basic uses twips. The measurements must be converted before using them together.

Private Sub Command3_Click()
    Dim RasterProc As New LEADRasterProcess
    'Declare the variable for printed text
    Dim Msg As String
    'Declare the variables for pixel measurements
    Dim TextHeightInPixels
    Dim UsableWidth
    Dim UsableHeight
    Dim MaxImageHeight
    'Declare the variables for sizing and positioning the image
    Dim PrintLeft As Long
    Dim PrintTop As Long
    Dim PrintHeight As Long
    Dim PrintWidth As Long
    'Declare variables used for preserving aspect ratios
    Dim WidthFactor
    Dim HeightFactor
    'Set the variables used for preserving the aspect ratio
    HeightFactor = LEADRasterView1.Raster.BitmapHeight
    WidthFactor = LEADRasterView1.Raster.BitmapWidth
    'Set the pointer to an hourglass
    Screen.MousePointer = 11
    'Get the page width and height in pixels (dots)
    UsableWidth = Printer.Width / Printer.TwipsPerPixelX
    UsableHeight = Printer.Height / Printer.TwipsPerPixelY
    'Print a title at the top of the page
    Msg = "This is a print example using LEADTOOLS COM."
    Printer.Print Msg
    'Get the maximum height of one image,
    'assuming two equal-size images and space for 12 lines of text
    TextHeightInPixels = TextHeight(Msg) / Printer.TwipsPerPixelY
    MaxImageHeight = (UsableHeight - (12 * TextHeightInPixels)) / 2
    'Size and position the first image, preserving the aspect ratio.
    'Check to see if using the maximum width will make the image too tall.
    'Set the dimensions based on the result.
    If ((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight Then
        PrintLeft = 1
        PrintTop = Printer.CurrentY / Printer.TwipsPerPixelX
        PrintWidth = UsableWidth
        PrintHeight = (PrintWidth * HeightFactor) / WidthFactor
    Else
        PrintLeft = 1
        PrintTop = Printer.CurrentY / Printer.TwipsPerPixelX
        PrintHeight = MaxImageHeight
        PrintWidth = (PrintHeight * WidthFactor) / HeightFactor
    End If
    'Print the first image
    LEADRasterView1.Render Printer.hdc, PrintLeft, PrintTop, PrintWidth, PrintHeight
    'Update the current printing position
    Printer.CurrentY = Printer.CurrentY + (PrintHeight * Printer.TwipsPerPixelY)
    'Print a blank line, then print a caption for the next picture
    Msg = " "
    Printer.Print Msg
    Msg = "This is the second picture, which is modified before printing:"
    Printer.Print Msg
    'Stretch the intensity of the bitmap for the second printing
    'to show the difference between the second picture and the first
    RasterProc.StretchIntensity LEADRasterView1.Raster
    'Size and position the second image, preserving the aspect ratio.
    'The coding is the same as for the first image.
    If ((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight Then
        PrintLeft = 1 'This is for flush left. Use 0 for centering.
        PrintTop = Printer.CurrentY / Printer.TwipsPerPixelX
        PrintWidth = UsableWidth
        PrintHeight = (PrintWidth * HeightFactor) / WidthFactor
    Else
        PrintLeft = 1 'This is for flush left. Use 0 for centering.
        PrintTop = Printer.CurrentY / Printer.TwipsPerPixelX
        PrintHeight = MaxImageHeight
        PrintWidth = (PrintHeight * WidthFactor) / HeightFactor
    End If
    'Print the second image
    LEADRasterView1.Render Printer.hdc, PrintLeft, PrintTop, PrintWidth, PrintHeight
    'Finish the page and finish the print job
    Printer.NewPage
    Printer.EndDoc
    'Set the mouse pointer back to the default
    Screen.MousePointer = 0
End Sub

5.

Run your program to test it.