Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrint a drawing that is centered and stretched to fit the page in VB .NET
DescriptionThis example shows how to print a drawing that is centered and stretched to fit the page in VB .NET. It shows how to define a transformation on the Graphics object to fit and center the drawing.
Keywordsprint, preview, center, stretch, fit, print preview, VB .NET
CategoriesGraphics, VB.NET
 
Subroutine FitPictureToMargins defines the transformation. It clears the Graphics object's current transformation if it has one. It then translates the picture rectangle so it is centered at the origin, scales so the picture rectangle is as large as possible while still fitting within the margins, and finally translates to move the center of the picture rectangle from the origin to the center of the margin rectangle.
 
' 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
 
The following code shows how the program uses this routine. It draws the untransformed margins for debugging purposes. Then it calls FitPictureToMargins passing it the picture's bounds and the margins' bounds. Next it calls subroutine DrawFace to draw the graphics within the indicate picture bounds. The Graphics object's transformation automatically centers and fits the results to the page.
 
' Print the page.
Private Sub Print_PrintPage(ByVal sender As Object, ByVal e _
    As System.Drawing.Printing.PrintPageEventArgs)
    ' Draw the margins (for debugging). Be sure 
    ' to do this before transforming the Graphics object.
    e.Graphics.DrawRectangle(Pens.Red, e.MarginBounds)

    ' We draw in the area (0, 0) - (100, 100). 
    ' Transform the Graphics object to center the results.
    Dim picture_rect As New RectangleF(0, 0, 100, 100)
    Dim margin_rect As New RectangleF( _
        e.MarginBounds.X, _
        e.MarginBounds.Y, _
        e.MarginBounds.Width, _
        e.MarginBounds.Height)
    FitPictureToMargins(e.Graphics, picture_rect, _
        margin_rect)

    ' Draw the graphics.
    DrawFace(e.Graphics)

    ' There are no more pages.
    e.HasMorePages = False
End Sub
 
This program prints or previews the result.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated