Printing Multiple Images (Delphi)

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\btndbtn.gif Select the Button 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.

Change the Button control's Caption property to Print 2 on 1 Page.

4.

Add Printers to the Uses section of the main unit.

5.

Code the Print 2 on 1 Page button's Click procedure as follows. In online help, you can copy the block of code and paste it into your application.

procedure TForm1.Button1Click(Sender: TObject);

var
{Declare the variable for printed text.}
Msg: String;

{Declare the variables for print-area measurements.}
SkipSpace: Integer;
UsableWidth, UsableHeight, MaxImageHeight: Single;

{Declare the variables for sizing and positioning the image.}
PrintLeft, PrintTop, PrintHeight, PrintWidth: Integer;

{Declare variables used for preserving aspect ratios.}
WidthFactor, HeightFactor: Single;

begin
{Set the variables used for preserving the aspect ratio}
HeightFactor := Lead1.BitmapHeight;
WidthFactor := Lead1.BitmapWidth;

{Set the pointer to an hourglass}
Screen.Cursor := crHourglass;

{Establish some print-area dimensions. These are arbitrary values.}
SkipSpace := round(Printer.PageHeight / 30);
UsableWidth := round(Printer.PageWidth) - (SkipSpace * 4);
UsableHeight := round(Printer.PageHeight) - (SkipSpace * 4);

{Get the maximum height of one image,}
{assuming two equal-size images and space between them.}
MaxImageHeight := round( (UsableHeight - (2 * SkipSpace)) / 2);

{Print a title at the top of the page}
Msg := 'This is a print example using the LEADTOOLS VCL.';
Printer.BeginDoc;
Printer.Canvas.TextOut( SkipSpace * 2, SkipSpace * 2, Msg);

{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 := Round(SkipSpace * 2);
    PrintTop := Round(Printer.Canvas.PenPos.Y + SkipSpace);
    PrintWidth := Round(UsableWidth);
    PrintHeight := Round((PrintWidth * HeightFactor) / WidthFactor);
end
Else
begin
    PrintLeft := Round(SkipSpace * 2);
    PrintTop := Round(Printer.Canvas.PenPos.Y + SkipSpace);
    PrintHeight := Round(MaxImageHeight);
    PrintWidth := Round((PrintHeight * WidthFactor) / HeightFactor);
End;

{Print the first image}
Lead1.Render(Printer.Canvas.HANDLE, PrintLeft, PrintTop, PrintWidth, PrintHeight);

{Update the current printing position}
Printer.Canvas.MoveTo( SkipSpace, Printer.Canvas.PenPos.Y +
                   (PrintHeight) + (2 * SkipSpace));

{Print the second line of text.}
Msg := 'This is the second picture, which is modified before printing:';
Printer.Canvas.TextOut( SkipSpace * 2, Printer.Canvas.PenPos.Y, Msg);

{Modify the bitmap for the second printing}
{to show the difference between the second picture and the first.}
Lead1.HistoContrast(200);

{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 := Round(SkipSpace * 2);
    PrintTop := Round(Printer.Canvas.PenPos.Y + SkipSpace);
    PrintWidth := Round(UsableWidth);
    PrintHeight := Round((PrintWidth * HeightFactor) / WidthFactor);
end
Else
begin
    PrintLeft := Round(SkipSpace * 2);
    PrintTop := Round(Printer.Canvas.PenPos.Y + SkipSpace);
    PrintHeight := Round(MaxImageHeight);
    PrintWidth := Round((PrintHeight * WidthFactor) / HeightFactor);
End;

{Print the second image}
Lead1.Render(Printer.Canvas.HANDLE, PrintLeft, PrintTop, PrintWidth, PrintHeight);

{Finish the page and finish the print job}
Printer.EndDoc;

{Set the mouse pointer back to the default}
Screen.Cursor := crDefault;

end;

6.

Run your program to test it.