Writing and Reading Multipage Files (Delphi)
Take the following steps to create a multipage file, then load and display the images from the file. For a list of formats that support multipage file operations, see the iModify parameter of the Save method.
1. |
Start Delphi. |
2. |
In the File menu, choose New Application. |
3. |
On the Delphi toolbar, click the LEADTOOLS tab. If you have used a LEAD VCL control before, the icon appears on the toolbar. Otherwise, refer to Installing VCL before continuing with this tutorial. |
4. |
|
5. |
Add the LEAD VCL LEADDEF unit to the uses section of unit1. |
6. |
Code the main form's FormShow procedure as follows. In online help, you can copy the block of code and paste it into your application. For a general explanation of how images are positioned and scaled, refer to Displaying an Image. |
procedure TForm1.FormShow(Sender: TObject);
var
HeightFactor1, WidthFactor1, HeightFactor2, WidthFactor2: Single;
HeightAllowed, WidthAllowed: Single;
ImageWidth1, ImageWidth2, ImageHeight1, ImageHeight2: Integer;
begin
{Disable automatic repainting of the image.}
Lead1.AutoRepaint := False;
Lead2.AutoRepaint := False;
{Turn off scroll bars to make sure we use the full client area.}
Lead1.AutoScroll := False;
Lead2.AutoScroll := False;
{ Position and size the main form so that it takes up most of the screen. }
Width := Round(Screen.Width * 0.9);
Height := Round(Screen.Height* 0.8);
Left := Round((Screen.Width - Width) / 2);
Top := Round((Screen.Height - Height) / 2);
{Display the form and set the mouse pointer to an hourglass.}
Show;
Screen.Cursor := crHourglass;
{Load the bitmaps. These hard-coded path names may be different on your system.}
Lead1.Load('C:\lead\IMAGES\IMAGE1.CMP', 0, 0, 1);
Lead2.Load('C:\lead\IMAGES\IMAGE2.CMP', 0, 0, 1);
{Save the bitmaps to a single multipage TIFF file}
Lead1.Save('C:\lead\IMAGES\COMBINED.MPT', FILE_TIF, 24, 0, SAVE_OVERWRITE);
Lead2.Save('C:\lead\IMAGES\COMBINED.MPT', FILE_TIF, 24, 0, SAVE_APPEND);
{Get information about the images so that we can size the controls.}
Lead1.GetFileInfo('C:\lead\IMAGES\COMBINED.MPT', 1);
Lead2.GetFileInfo('C:\lead\IMAGES\COMBINED.MPT', 2);
{Clear the bitmaps from memory}
Lead1.Bitmap := 0;
Lead2.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.}
Lead1.Visible := True;
Lead2.Visible := True;
{Set the variables used for preserving the aspect ratio.}
HeightFactor1 := Lead1.InfoHeight;
WidthFactor1 := Lead1.InfoWidth;
HeightFactor2 := Lead2.InfoHeight;
WidthFactor2 := Lead2.InfoWidth;
HeightAllowed := ClientHeight * 0.8;
WidthAllowed := ClientWidth * 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
Lead1.Left := Round((ClientWidth / 4) - (WidthAllowed / 2));
Lead1.Width := Round(WidthAllowed);
Lead1.Height := Round((Lead1.Width * HeightFactor1) / WidthFactor1);
Lead1.Top := Round((ClientHeight - Lead1.Height) / 2);
end
Else
begin
Lead1.Top := Round((ClientHeight - HeightAllowed) / 2);
Lead1.Height := Round(HeightAllowed);
Lead1.Width := Round((Lead1.Height * WidthFactor1) / HeightFactor1);
Lead1.Left := Round((ClientWidth / 4) - (Lead1.Width / 2) );
End;
If (WidthAllowed * HeightFactor2) / WidthFactor2 < HeightAllowed Then
begin
Lead2.Left := Round((ClientWidth * 3/4) - (WidthAllowed / 2) );
Lead2.Width := Round(WidthAllowed);
Lead2.Height := Round((Lead2.Width * HeightFactor2) / WidthFactor2);
Lead2.Top := Round((ClientHeight - Lead2.Height) / 2);
end
Else
begin
Lead2.Top := Round((ClientHeight - HeightAllowed) / 2);
Lead2.Height := Round(HeightAllowed);
Lead2.Width := Round((Lead2.Height * WidthFactor2) / HeightFactor2);
Lead2.Left := Round((ClientWidth * 3 / 4) - (Lead2.Width / 2) );
End;
{Load the bitmaps from the multipage TIFF file}
Lead1.Load('C:\lead\IMAGES\COMBINED.MPT', 24, 1, 1);
Lead2.Load('C:\lead\IMAGES\COMBINED.MPT', 24, 2, 1);
{Set the image display sizes to match the LEAD controls}
ImageWidth1 := Lead1.Width;
ImageHeight1 := Lead1.Height;
Lead1.SetDstRect( 0, 0, ImageWidth1, ImageHeight1);
Lead1.SetDstClipRect(0, 0, ImageWidth1, ImageHeight1);
{Set the image display sizes to match the LEAD controls}
ImageWidth2 := Lead2.Width;
ImageHeight2 := Lead2.Height;
Lead2.SetDstRect(0, 0, ImageWidth2, ImageHeight2);
Lead2.SetDstClipRect(0, 0, ImageWidth2, ImageHeight2);
{Display the images}
Lead1.ForceRepaint();
Lead2.ForceRepaint();
{Set the mouse pointer back to the default}
Screen.Cursor := crDefault;
End;
7. |
Run your program to test it. |