Printing Multiple Images (C++ Builder)

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 #include <printers.hpp> to 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.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   //Declare the variable for printed text.
   AnsiString Msg;
   //Declare the variables for print-area measurements.
   int SkipSpace;
   float UsableWidth, UsableHeight, MaxImageHeight;
   //Declare the variables for sizing and positioning the image.
   int PrintLeft, PrintTop, PrintHeight, PrintWidth;
   //Declare variables used for preserving aspect ratios.
   float WidthFactor, HeightFactor;

   //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 = Printer()->PageHeight / 30;
   UsableWidth = Printer()->PageWidth - (SkipSpace * 4);
   UsableHeight = Printer()->PageHeight - (SkipSpace * 4);
   //Get the maximum height of one image, 
   //assuming two equal-size images and space between them.
   MaxImageHeight = (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 makes the image too tall.
   //Set the dimensions based on the result.
   if(((UsableWidth * HeightFactor) / WidthFactor) < MaxImageHeight)
   {
      PrintLeft = SkipSpace * 2;
      PrintTop = Printer()->Canvas->PenPos.y + SkipSpace;
      PrintWidth = UsableWidth;
      PrintHeight = (PrintWidth * HeightFactor) / WidthFactor;
   }
   else
   {
      PrintLeft = SkipSpace * 2;
      PrintTop = Printer()->Canvas->PenPos.y + SkipSpace;
      PrintHeight = MaxImageHeight;
      PrintWidth = (PrintHeight * WidthFactor) / HeightFactor;
   }
   //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 modif(ied before printing:";
   Printer()->Canvas->TextOut( SkipSpace * 2, Printer()->Canvas->PenPos.y, Msg);
   //Modif(y the bitmap for the second printing
   //to show the dif(ference 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)
   {
      PrintLeft = SkipSpace * 2;
      PrintTop = Printer()->Canvas->PenPos.y + SkipSpace;
      PrintWidth = UsableWidth;
      PrintHeight = (PrintWidth * HeightFactor) / WidthFactor;
   }
   else
   {
      PrintLeft = SkipSpace * 2;
      PrintTop = Printer()->Canvas->PenPos.y + SkipSpace;
      PrintHeight = MaxImageHeight;
      PrintWidth = (PrintHeight * WidthFactor) / HeightFactor;
   }
   //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;
}

6.

Run your program to test it.