Dynamically Positioning the Control (C++ 5.0 and later)

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 OnLoadlead procedure so that it appears as follows. For a general explanation of how images are positioned and scaled, refer to Displaying an Image.

   // Declare local variables.
   float HeightFactor, WidthFactor, HeightAllowed, WidthAllowed;
   float Width, Height, Left, Top;
   CString ImageFile;
   ILEADRasterIO *pRasterIO=NULL;

   CoCreateInstance(CLSID_LEADRasterIO, NULL, CLSCTX_ALL, IID_ILEADRasterIO, (void**)&pRasterIO);

   // Load the bitmap. This hard-coded path name may be different on your system.
   /******* Put a valid image path here **********/
   ImageFile = TEXT("d:\\temp\\24bit.bmp");
   /**********************************************/
   // Get file information
   pRasterIO->GetFileInfo(m_LEADRasterView1.GetRaster(), LPCTSTR(ImageFile), 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->GetInfoHeight();
   WidthFactor = pRasterIO->GetInfoWidth();
   CRect rcWindow;
   GetClientRect(rcWindow);
   HeightAllowed = rcWindow.Height() * 3.0f / 4;
   WidthAllowed = rcWindow.Width() * 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)
   {
     Left = rcWindow.Width() / 8.0f;
     Width = WidthAllowed;
     Height = (Width * HeightFactor) / WidthFactor;
     Top = (rcWindow.Height() - Height) / 2;
   }
   else
   {
     Top = rcWindow.Height() / 8.0f;
     Height = HeightAllowed;
     Width = (Height * WidthFactor) / HeightFactor;
     Left = (rcWindow.Width() - Width) / 2;
   }
   rcWindow.SetRect(0, 0, (int) Width, (int) Height);
   rcWindow.OffsetRect((int) Left, (int) Top);
   m_LEADRasterView1.MoveWindow(rcWindow);
   // Set up for handling our own display rectangles.
   m_LEADRasterView1.SetAutoScroll(FALSE);
   m_LEADRasterView1.SetAutoRepaint(TRUE);
   // Load the bitmap
   pRasterIO->Load(m_LEADRasterView1.GetRaster(), LPCTSTR(ImageFile), 0, 0, 1);
   // Set the image display size to match the LEAD control
   m_LEADRasterView1.SetDstRect(0.0f, 0.0f,
                     m_LEADRasterView1.GetScaleWidth(),
                     m_LEADRasterView1.GetScaleHeight());
   m_LEADRasterView1.SetDstClipRect(0.0f, 0.0f,
                     m_LEADRasterView1.GetScaleWidth(),
                     m_LEADRasterView1.GetScaleHeight());
   pRasterIO->Release();

3.

Rebuild and run the application.