Dynamically Positioning the Control (C++ Builder 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 an Image.

void __fastcall TForm1::LoadLeadClick(TObject *Sender)
{
  ILEADRasterIO * pRasterIO;
 long int HeightFactor;
 long int WidthFactor;
 long int HeightAllowed;
 long int WidthAllowed;

CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);
// Get file information
pRasterIO->GetFileInfo (LEADRasterView1->Raster, AnsiToOLESTR("v:\\images\\image1.cmp"), 0, 0);
// 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 = pRasterIO->InfoHeight;
WidthFactor = pRasterIO->InfoWidth;
HeightAllowed = ClientHeight * 3.0f / 4;
WidthAllowed = ClientWidth * 3.0f / 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)
{
LEADRasterView1->Left = ClientWidth / 8.0f;
LEADRasterView1->Width = WidthAllowed;
LEADRasterView1->Height = (Width * HeightFactor) / WidthFactor;
LEADRasterView1->Top = (ClientHeight - Height) / 2;
}
else
{
LEADRasterView1->Top = ClientHeight / 8.0f;
LEADRasterView1->Height = HeightAllowed;
LEADRasterView1->Width = (Height * WidthFactor) / HeightFactor;
LEADRasterView1->Left = (ClientWidth - Width) / 2;
}
// Set up for handling our own display rectangles.
LEADRasterView1->AutoScroll = false;
LEADRasterView1->AutoRepaint = true;
// Load the bitmap
pRasterIO->Load (LEADRasterView1->Raster, AnsiToOLESTR("v:\\images\\image1.cmp"), 0, 0, 1);
// Set the image display size to match the LEAD control
LEADRasterView1->SetDstRect (0.0f, 0.0f,
LEADRasterView1->ScaleWidth,
LEADRasterView1->ScaleHeight);
LEADRasterView1->SetDstClipRect (0.0f, 0.0f,
LEADRasterView1->ScaleWidth,
LEADRasterView1->ScaleHeight);
pRasterIO->Release();
}

3.

Run your program to test it.