Printing Multiple Images (Delphi 4)

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 a Button control; and add it to your main form. Put it at the top of the form to keep it away from the image.

3.

Change the Button control's Caption to Print Twice on One Page.

4.

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

procedure TForm1.Button1Click(Sender: TObject);
var
   RasterProc: LEADRasterProcess;
   //Declare the variable for printed text
   Msg: String;
   //Declare the variables for pixel measurements
   TextHeightInPixels: Integer;
   UsableWidth: Integer;
   UsableHeight: Integer;
   MaxImageHeight: Integer;
   //Declare the variables for sizing and positioning the image
   PrintLeft: longint;
   PrintTop: longint;
   PrintHeight: longint;
   PrintWidth: longint;
   //Declare variables used for preserving aspect ratios
   WidthFactor: Integer;
   HeightFactor: Integer;
   Ret: Smallint;
begin
   RasterProc := CreateComObject ( CLASS_LEADRasterProcess ) as LEADRasterProcess;
   //Set the variables used for preserving the aspect ratio
   HeightFactor := Trunc (LEADRasterView1.Raster.BitmapHeight);
   WidthFactor:= Trunc (LEADRasterView1.Raster.BitmapWidth);

     //Set the pointer to an hourglass
   Screen.Cursor := crHourGlass;
   //Get the page width and height in pixels (dots)
   UsableWidth := Printer.PageWidth ;
   UsableHeight := Printer.PageHeight ;
   //Print a title at the top of the page
   Msg:= 'This is a print example using LEADTOOLS ActiveX.';
   Printer.BeginDoc ( );
   Printer.Canvas.TextOut ( Printer.Canvas.PenPos.x, Printer.Canvas.PenPos.y, Msg ) ;
   //Get the maximum height of one image,
   //assuming two equal-size images and space for 12 lines of text
   TextHeightInPixels := Printer.Canvas.TextHeight (Msg);
   MaxImageHeight := Trunc ( (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
   begin
      PrintLeft := 1;
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y;
      PrintWidth := UsableWidth;
      PrintHeight := Trunc ((PrintWidth * HeightFactor) / WidthFactor);
   end
   else
   begin
      PrintLeft := 1;
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y ;
      PrintHeight := MaxImageHeight;
      PrintWidth := Trunc ( (PrintHeight * WidthFactor) / HeightFactor);
   end;
   //Print the first image
   LEADRasterView1.Render ( Printer.Handle, PrintLeft, PrintTop, PrintWidth, PrintHeight, Ret ) ;
   //Update the current printing position
   Printer.Canvas.MoveTo ( 1, Printer.Canvas.PenPos.y + PrintHeight + TextHeightInPixels );
   //Print a blank line, then print a caption for the next picture
   Msg := ' ' ;
   Printer.Canvas.TextOut ( Printer.Canvas.PenPos.x, Printer.Canvas.PenPos.y, Msg ) ;
   Msg := 'This is the second picture, which is modified before printing:';
   Printer.Canvas.TextOut ( Printer.Canvas.PenPos.x, Printer.Canvas.PenPos.y + TextHeightInPixels, 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
   begin
      PrintLeft := 1; //This is for flush left. Use 0 for centering.
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y ;
      PrintWidth := UsableWidth ;
      PrintHeight := Trunc((PrintWidth * HeightFactor) / WidthFactor);
   end
   else
   begin
      PrintLeft := 1 ;//This is for flush left. Use 0 for centering.
      PrintTop := TextHeightInPixels + Printer.Canvas.PenPos.y ;
      PrintHeight := MaxImageHeight;
      PrintWidth := Trunc((PrintHeight * WidthFactor) / HeightFactor);
   end;
    //Print the second image
   LEADRasterView1.Render ( Printer.Handle, PrintLeft, PrintTop, PrintWidth, PrintHeight, Ret ) ;
   //Finish the page and finish the print job
   Printer.NewPage ( ) ;
   Printer.EndDoc ( ) ;
   //Set the mouse pointer back to the default
   Screen.Cursor := crDefault;
end;

5.

On the Project pull-down menu, use the Import Type library… and select the LEAD Raster Process object library (14.5).

 

At the beginning of the Unit1 file, add LTRASTERPROCLib_TLB, Printers to the uses section. For example:

Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, LTRASTERPROCLib_TLB, Printers;

6.

Run your program to test it.