Print a Real Image Size in Inches (Delphi 6.0)

Take the following steps to start a project and to add some code that prints an image:

1.

Start Delphi 6.

2.

If you didn’t install LEAD RasterView Control, install it as follows:

 

On the Component pull-down menu, use the Import ActiveX Control… and select the LEAD RasterView Control (14.5). Press Install, and then compile and install the package dclusr.dpk.

3.

image\btnlead.gif Select the LEAD RasterView control and add it to your form. Size and position the control, as you want it to appear at run time.

4.

If you didn’t import LEAD Raster IO before, import it as follows:

 

On the Project pull-down menu, use the Import Type library… and select the LEAD Raster IO Object Library (14.5). Press Install, and then compile and install the package dclusr.dpk.

5.

From the ActiveX controls tab, add the LEAD Raster IO control to your form.

6.

Add a button control to your form and name it as follows:

 

Name

Caption

 

btnPrint

Print

7.

Declare the following variables in the private section of Unit1.

fRealWidth: Single; //Image real width in inches
fRealHeight: Single;// Image real height in inches
XRes: Integer;      // Number of pixels per logical inch along the screen width.
YRes: Integer;     // Number of pixels per logical inch along the screen Height.

8.

Handle the btnPrint button's click procedure and code the btnPrintClick procedure as follows. This code loads an image.

procedure TForm1.btnPrintClick(Sender: TObject);
begin
      LEADRasterIO1.Load ( LEADRasterView1.Raster, 'c:\image1.cmp', 0, 0, 1); 
end;

9.

You should now calculate the real width and height of the bitmap in inches. Add the following code to btnPrintClick procedure:

   fRealWidth:= LEADRasterView1.Raster.BitmapWidth / LEADRasterView1.Raster.BitmapXRes
   fRealHeight:= LEADRasterView1.Raster.BitmapHeight / LEADRasterView1.Raster.BitmapYRes

10.

Now you are ready to print the image with real size in inches. Add the following code to btnPrintClick procedure:

   //You should have the number of pixels per logical inch (XRes, YRes) 
   XRes:= GetDeviceCaps ( Printer.Canvas.Handle, LOGPIXELSX ) ; 
   YRes:= GetDeviceCaps ( Printer.Canvas.Handle, LOGPIXELSY ) ; 

   LEADRasterView1.Render ( Printer.Canvas.Handle, 
                               1, 
                               1, 
                               Trunc(XRes * fRealWidth), 
                               Trunc(YRes * fRealHeight) ) ; 
   Printer.EndDoc;

11.

Add "Printers" to the Uses section of the Uint1.

12.

Run your program to test it.