Print a drawing that is centered and stretched to fit the page in VB .NET

Description

This 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.

Keywords

print, preview, center, stretch, fit, print preview, VB .NET

Categories

Graphics, 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.

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.