Writing and Reading Multi-Page Files (Delphi 4)
Take the following steps to create a Multi-Page file, then load and display the images from the file. Only TIFF (MPT) and PCX (DCX) formats support Multi-Page files.
1. |
Start Delphi. |
|
2. |
If you didn’t install LEAD RasterView Control, install it. |
|
|
On the Component pull-down menu, use the Import ActiveX Control… and select the LEAD RasterView Control (14.5), and Press install. |
|
3. |
Select the LEAD RasterView control; then add two controls to your main form. The size and position does not matter. |
|
4. |
Add a button to your form and name it as follows: |
|
|
Name |
Caption |
|
Button1 |
Load Multi-Page File |
5. |
Code the Button1 Click’s procedure as the following. In online help, you can use the Edit pull-down menu to copy the block of code. This code loads two bitmaps, saves them both into a single Multi-Page TIFF file, loads them again from the TIFF file, and displays them side-by-side. |
procedure TForm1.Button1Click(Sender: TObject);
var
RasterIO1: LEADRasterIO;
RasterIO2: LEADRasterIO;
HeightFactor1, WidthFactor1,
HeightFactor2, WidthFactor2,
HeightAllowed, WidthAllowed: Integer;
ImageWidth1, ImageHeight1: Integer;
ImageWidth2, ImageHeight2: integer;
Ret: Smallint;
begin
RasterIO1:= CreateComObject(CLASS_LEADRasterIO) as LEADRasterIO;
RasterIO2:= CreateComObject(CLASS_LEADRasterIO) as LEADRasterIO;
//Disable automatic repainting of the image.
LEADRasterView1.AutoRepaint := False;
LEADRasterView2.AutoRepaint := False;
//Turn off scroll bars to make sure we use the full client area.
LEADRasterView1.AutoScroll := False;
LEADRasterView2.AutoScroll := False;
//Position and size the main form so that it takes up most of the screen.
Width := Trunc(Screen.Width * 0.9) ;
Height:= Trunc(Screen.Height* 0.8) ;
Left := Trunc ((Screen.Width- Width) / 2);
Top := Trunc ((Screen.Height- Height)/ 2);
//Display the form and set the mouse pointer to an hourglass.
Screen.Cursor := crhourGlass;
//Load the bitmaps. These hard-coded path names may be different on your system.
RasterIO1.Load ( LEADRasterView1.Raster, WideString('v:\images\image1.cmp'), 0, 0, 1 ) ;
RasterIO2.Load ( LEADRasterView2.Raster, WideString('v:\images\image2.cmp'), 0, 0, 1 ) ;
//Save the bitmaps to a single multipage TIFF file
RasterIO1.Save ( LEADRasterView1.Raster, WideString('v:\images\combined.mpt'), FILE_TIF, 24, 0, SAVE_OVERWRITE ) ;
RasterIO2.Save ( LEADRasterView2.Raster, WideString('v:\images\combined.mpt'), FILE_TIF, 24, 0, SAVE_APPEND );
//Get information about the images so that we can size the controls.
RasterIO1.GetFileInfo ( LEADRasterView1.Raster, WideString('v:\images\combined.mpt'), 1, 0 ) ;
RasterIO2.GetFileInfo ( LEADRasterView2.Raster, WideString('v:\images\combined.mpt'), 2, 0 );
//Clear the bitmaps from memory
LEADRasterView1.Raster.Bitmap := 0;
LEADRasterView2.Raster.Bitmap := 0;
//Make the controls visible so that we can size and position them.
//They will not really appear until we load an image and paint it.
LEADRasterView1.Visible := True;
LEADRasterView2.Visible := True;
//Set the variables used for preserving the aspect ratio.
HeightFactor1 := Trunc (RasterIO1.InfoHeight);
WidthFactor1 := Trunc (RasterIO1.InfoWidth);
HeightFactor2 := Trunc (RasterIO2.InfoHeight);
WidthFactor2 := Trunc (RasterIO2.InfoWidth);
HeightAllowed := Trunc (Height * 0.8);
WidthAllowed := Trunc (Width * 0.45);
//Center each LEAD control on half of the form, 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 (WidthAllowed * HeightFactor1) / WidthFactor1 < HeightAllowed then
begin
LEADRasterView1.Left := Trunc ((Width / 4) - (WidthAllowed / 2));
LEADRasterView1.Width := WidthAllowed;
LEADRasterView1.Height := Trunc ((LEADRasterView1.Width * HeightFactor1) / WidthFactor1);
LEADRasterView1.Top := Trunc ((Height - LEADRasterView1.Height) / 2);
end
else
begin
LEADRasterView1.Top := Trunc ((Height - HeightAllowed) / 2);
LEADRasterView1.Height := HeightAllowed;
LEADRasterView1.Width := Trunc ((LEADRasterView1.Height * WidthFactor1) / HeightFactor1);
LEADRasterView1.Left := Trunc ((Width / 4) - (LEADRasterView1.Width / 2));
end;
if (WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed then
begin
LEADRasterView2.Left := Trunc ((Width * 3 / 4) - (WidthAllowed / 2));
LEADRasterView2.Width := WidthAllowed;
LEADRasterView2.Height := Trunc ((LEADRasterView2.Width * HeightFactor2) / WidthFactor2);
LEADRasterView2.Top := Trunc ((Height - LEADRasterView2.Height) / 2);
end
else
begin
LEADRasterView2.Top := Trunc ((Height - HeightAllowed) / 2);
LEADRasterView2.Height := HeightAllowed;
LEADRasterView2.Width := Trunc ((LEADRasterView2.Height * WidthFactor2) / HeightFactor2);
LEADRasterView2.Left := Trunc ((Width * 3 / 4) - (LEADRasterView2.Width / 2));
end;
//Load the bitmaps from the multipage TIFF file
RasterIO1.Load ( LEADRasterView1.Raster, 'v:\images\combined.mpt', 24, 1, 1);
RasterIO2.Load ( LEADRasterView2.Raster, 'v:\images\combined.mpt', 24, 2, 1);
//Set the image display sizes to match the LEAD controls
ImageWidth1 := Trunc (LEADRasterView1.ScaleWidth);
ImageHeight1 := Trunc (LEADRasterView1.ScaleHeight);
LEADRasterView1.SetDstRect ( 0, 0, ImageWidth1, ImageHeight1, Ret ) ;
LEADRasterView1.SetDstClipRect ( 0, 0, ImageWidth1, ImageHeight1, Ret ) ;
//Set the image display sizes to match the LEAD controls
ImageWidth2 := Trunc (LEADRasterView2.ScaleWidth);
ImageHeight2 := Trunc (LEADRasterView2.ScaleHeight);
LEADRasterView2.SetDstRect ( 0, 0, ImageWidth2, ImageHeight2, Ret ) ;
LEADRasterView2.SetDstClipRect (0, 0, ImageWidth2, ImageHeight2, Ret ) ;
//Display the images
LEADRasterView1.ForceRepaint (Ret) ;
LEADRasterView2.ForceRepaint(Ret) ;
//Set the mouse pointer back to the default
Screen.Cursor:= crDefault;
end;
6. |
On the Project pull-down menu, use the Import Type library… and select the LEAD Raster IO object library (14.5). |
7. |
At the beginning of the Unit1 file, add ComObj, LTRASTERIOLib_TLB to the uses section. In order to deal with the Com objects, and deal with Raster IO object library, you must add the ComObj, LTRASTERIOLib_TLB units to the uses section. For example: |
Uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComObj, LTRASTERIOLib_TLB;
8. |
Run your program to test it. |