Dynamically Positioning the Control (Delphi 4.0)
Take the following steps to dynamically scale and position the LEAD RasterView control when loading an image. This example scales the control to use most of the form's area while preserving the image's aspect ratio.
1. |
Start with the project that you created in Loading and Displaying an Image. |
2. |
Modify the LeadLoad button's click procedure so that it appears as follows. For a general explanation of how images are positioned and scaled, refer to Displaying anImage. |
procedure TForm1.LoadLeadClick(Sender: TObject);
var
RasterIO: ILEADRasterIO;
HeightFactor: longint;
WidthFactor: longint;
HeightAllowed: longint;
WidthAllowed: longint;
Ret: smallint;
begin
RasterIO := CreateComObject (CLASS_LEADRasterIO) as ILEADRasterIO;
//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);
//Turn off automated behavior while we load and manipulate the image.
LEADRasterView1.AutoRepaint := False;
LEADRasterView1.AutoScroll := False;
LEADRasterView1.BackErase := False;
//Load the bitmap. This hard-coded path name may be different on your system.
RasterIO.Load ( LEADRasterView1.Raster, 'v:\images\Image1.cmp', 0, 0, 1 ) ;
//Set the variables used for preserving the aspect ratio.
//Allow for a border of 1/8 of the form size.
//The units of measure do not matter, since we are calculating proportions.
HeightFactor := Trunc ( LEADRasterView1.Raster.BitmapHeight);
WidthFactor := Trunc ( LEADRasterView1.Raster.BitmapWidth);
HeightAllowed := trunc ( Height * 3/4);
WidthAllowed := trunc ( Width * 3/4);
//Center the LEAD control on 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 * HeightFactor) / WidthFactor) < HeightAllowed Then
begin
LEADRasterView1.Left := Trunc ( Width / 8);
LEADRasterView1.Width := WidthAllowed;
LEADRasterView1.Height := Trunc ( (LEADRasterView1.Width * HeightFactor) / WidthFactor);
LEADRasterView1.TOP := TRunc((Height - LEADRasterView1.Height) / 2);
end
else
begin
LEADRasterView1.TOP := Trunc ( Height / 8 ) ;
LEADRasterView1.Height := HeightAllowed;
LEADRasterView1.Width := Trunc ( (LEADRasterView1.Height * WidthFactor) / HeightFactor);
LEADRasterView1.Left := Trunc((Width - LEADRasterView1.Width) / 2);
end;
//Set the image display size to match the LEAD control
LEADRasterView1.SetDstRect ( 0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight, Ret);
LEADRasterView1.SetDstClipRect ( 0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight, Ret);
LEADRasterView1.SetSrcRect ( 0, 0, LEADRasterView1.Raster.BitmapWidth, LEADRasterView1.Raster.BitmapHeight, Ret);
LEADRasterView1.SetSrcClipRect ( 0, 0, LEADRasterView1.Raster.BitmapWidth, LEADRasterView1.Raster.BitmapHeight, Ret);
//Turn on automated behavior while we load and manipulate the image.
LEADRasterView1.AutoRepaint := True;
LEADRasterView1.AutoScroll := True;
end;
3. |
Run your program to test it. |