Dynamically Positioning the Control (Visual Basic)

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.

Private Sub LoadLead_Click()
  Dim RasterIO As New LEADRasterIO
  'Declare variables used for preserving aspect ratios.
  Dim HeightFactor As Long
  Dim WidthFactor As Long
  Dim HeightAllowed As Long
  Dim WidthAllowed As Long
  'Position and size the main form so that it takes up most of the screen.
  Width = Screen.Width  * 0.9
  Height = Screen.Height* 0.8
  Left = (Screen.Width - Width) / 2
  TOP = (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\arcade.bmp", 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 = LEADRasterView1.Raster.BitmapHeight
  WidthFactor = LEADRasterView1.Raster.BitmapWidth
  HeightAllowed = ScaleHeight * 3/4
  WidthAllowed = ScaleWidth * 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
    LEADRasterView1.Left = ScaleWidth / 8
    LEADRasterView1.Width = WidthAllowed
    LEADRasterView1.Height = (LEADRasterView1.Width * HeightFactor) / WidthFactor
    LEADRasterView1.TOP = (ScaleHeight - LEADRasterView1.Height) / 2
  Else
    LEADRasterView1.TOP = ScaleHeight / 8
    LEADRasterView1.Height = HeightAllowed
    LEADRasterView1.Width = (LEADRasterView1.Height * WidthFactor) / HeightFactor
    LEADRasterView1.Left = (ScaleWidth - LEADRasterView1.Width) / 2
  End If
  'Set the image display size to match the LEAD control
  LEADRasterView1.SetDstRect 0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight
  LEADRasterView1.SetDstClipRect 0, 0, LEADRasterView1.ScaleWidth, LEADRasterView1.ScaleHeight
  LEADRasterView1.SetSrcRect 0, 0, LEADRasterView1.Raster.BitmapWidth, LEADRasterView1.Raster.BitmapHeight
  LEADRasterView1.SetSrcClipRect 0, 0, LEADRasterView1.Raster.BitmapWidth, LEADRasterView1.Raster.BitmapHeight
  'Turn on automated behavior while we load and manipulate the image.
  LEADRasterView1.AutoRepaint = True
  LEADRasterView1.AutoScroll = True
End Sub

3.

Run your program to test it.