Dynamically Positioning the Control (C++ Builder 6.0)

Take the following steps to dynamically scale and position the LEAD Raster View 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 btnLoadImage 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::btnLoadImageClick(TObject *Sender) 
{
   int HeightFactor; 
   int WidthFactor; 
   int HeightAllowed; 
   int WidthAllowed; 

   // Get file information
   LEADRasterIO1->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 = LEADRasterIO1->InfoHeight
   WidthFactor = LEADRasterIO1->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
   LEADRasterIO1->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); 
}

3.

Run your program to test it.