Fitting Pictures to the Page


Another common scenario is drawing a picture as large as possible on the page without distorting it. You can use the same approach to this problem that was described in the previous section: Apply a transformation to the PrintPage event handler’s Graphics object to make the picture fit the printed page.

The subroutine FitPictureToMargins shown in the following code makes this transformation. It begins by calling the Graphics object’s ResetTransform method to remove any existing transformation. Next the subroutine translates to center the picture_bounds rectangle at the origin. Scaling an object centered at the origin is relatively simple because the object’s center remains at the origin, so the program starts by centering picture_bounds.

The program compares aspect ratios (ratios of height/width) of the picture_bounds and margin_bounds rectangles. If picture_bounds has the greater aspect ratio, then it is relatively taller and thinner than margin_bounds. In that case, the program scales to make picture_bounds the same height as margin_ bounds and sets its width appropriately.

If picture_bounds has the smaller aspect ratio, it is relatively wider and shorter than margin_bounds. In that case, the program scales to make picture_bounds the same width as margin_bounds and sets its height accordingly.

After calculating the scale factor it needs, the program calls the Graphics object’s ScaleTransform method to add it to the Graphics object’s transformation. It uses the MatrixOrder.Append parameter to make the object apply the scaling transformation after its first translation.

Finally, the subroutine applies another translation to move the center of the scaled picture_bounds rectangle from the origin to the center of margin_bounds. It again uses the MatrixOrder.Append parameter, so the new transformation is applied after the previous ones.

  ' Transform the Graphics object to fit the rectangle ' picture_bounds to margin_bounds and center it. Private Sub FitPictureToMargins(ByVal gr As Graphics, _  ByVal picture_bounds As RectangleF, ByVal margin_bounds As RectangleF)     ' Remove any existing transformation.     gr.ResetTransform()     ' Translate to center picture_bounds at the origin.     gr.TranslateTransform( _         -(picture_bounds.Left + picture_bounds.Width / 2), _         -(picture_bounds.Top + picture_bounds.Height / 2))     ' Scale to make picture_bounds fit margin_bounds.     ' Compare the aspect ratios.     Dim margin_aspect As Single = margin_bounds.Height / margin_bounds.Width     Dim picture_aspect As Single = picture_bounds.Height / picture_bounds.Width     Dim scale As Single     If picture_aspect > margin_aspect Then         ' picture_bounds is relatively tall and thin.         ' Make it as tall as possible.         scale = margin_bounds.Height / picture_bounds.Height     Else         ' picture_bounds is relatively short and wide.         ' Make it as wide as possible.         scale = margin_bounds.Width / picture_bounds.Width     End If     ' Scale.     gr.ScaleTransform(scale, scale, MatrixOrder.Append)     ' Translate to move the origin to the center of margin_bounds.     gr.TranslateTransform( _         margin_bounds.Left + margin_bounds.Width / 2, _         margin_bounds.Top + margin_bounds.Height / 2, _         MatrixOrder.Append) End Sub 

A program can use subroutine FitPictureToMargins exactly as it can use subroutine CenterPictureInMargins. Figure 24-6 shows the result. This routine works whether the picture area is relatively short and wide (as in this case) or tall and thin. It will also shrink a picture that is bigger than the page.

image from book
Figure 24-6: Subroutine FitPictureToMargins makes it easy to center a picture within a printed page, making it as large as possible without distortion.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net