| 9/20/2006
PrintMultiplePageImageVisualBasic2005.zip
This article discusses how to print a
multiple page image and provides sample code in a Visual Studio 2005
solution. The process involves calculating an array of rectangles
where each rectangle defines a page size chunk of the image. The
rectangles are then used to extract a page sized chunk from the
image for each page to be printed.
Code Example One
Before it can be printed, a large
image must be divided into page size chunks. In the sample code this
is accomplished with the CalculatePages method:
Private
Sub CalculatePages(ByVal
theImage As Image)
' Declare a variable named
thePrinterSettings of type PrinterSettings;
' call the PrinterSettings class' New
constructor assigning the resulting
' PrinterSettings object to
thePrinterSettings variable.
Dim thePrinterSettings
As New
PrinterSettings
' Calcuate Page Width
Me._PageWidth = (thePrinterSettings.DefaultPageSettings.PaperSize.Width
- thePrinterSettings.DefaultPageSettings.Margins.Left -
thePrinterSettings.DefaultPageSettings.Margins.Right)
' Calculate Page Height
Me._PageHeight = (thePrinterSettings.DefaultPageSettings.PaperSize.Height
- thePrinterSettings.DefaultPageSettings.Margins.Top -
thePrinterSettings.DefaultPageSettings.Margins.Bottom)
' Calculate Pages Wide
Me._PagesWide =
CInt(Math.Ceiling((theImage.Width /
Me._PageWidth)))
' Calculate Pages Tall
Me._PagesTall =
CInt(Math.Ceiling((theImage.Height
/ Me._PageHeight)))
' Declare a variable of type Rectangle
array;
' set its capacity to hold a rectangle for
each of the pages to be printed.
Dim pagesRectangles((Me._PagesTall
* Me._PagesWide) - 1)
As Rectangle
' Declare variables to hold coordinates as
each pages rectangle is calculated.
Dim LeftX, LeftY, RightX, RightY
As Integer
' Declare variable to add rectangles to
pagesRectangle elements.
Dim rectNumber
As Integer
= 0
' Process the image into an array of
rectangles; each rectangle
' contains the coordinates to extract part
of the image to a printed page.
For row As
Integer = 0
To Me._PagesTall - 1
For col As
Integer = 0
To Me._PagesWide - 1
LeftX = col * Me._PageWidth
LeftY = row * Me._PageHeight
RightX = (LeftX + Me._PageWidth)
RightY = (LeftY + Me._PageHeight)
' Add a rectangle for the
page to pagesRectangles.
pagesRectangles(rectNumber) =
New Rectangle(LeftX, LeftY, RightX,
RightY)
rectNumber += 1
Next
Next
' Assign pagesRectangle object to _pagesRectangles
variable.
Me._pageRectangles = pagesRectangles
End
Sub
Code Example
TwoAfter the page
size chunks have converted into image coordinates they can be
used to print each page size chunk one at a time:
Private
Sub PrintImage(ByVal
sender As
Object,
ByVal e As
Printing.PrintPageEventArgs)
' Declare a variable named pageImage
of type Bitmap;
' Call the BitMap class' New
constructor assigning the resulting
' Bitmap object to the pageImage
variable.
Dim pageImage
As New
Bitmap(Me._PageWidth,
Me._PageHeight)
' Declare a variable named
pageImageGraphics of type Graphics;
' call the Graphics class' FromImage
method assigning the resulting
' Graphics object to the
pageImageGraphics variable.
Dim pageImageGraphics
As Graphics =
Graphics.FromImage(pageImage)
' Call thePageImageGraphics object's
DrawImage method to draw
' a portion of the image being printed
to the pageImage BitMap.
pageImageGraphics.DrawImage(Me._LargeImage,
0, 0, Me._pageRectangles(Me._PagesPrinted),
GraphicsUnit.Pixel)
' Call the PrintPageEventArgs
DrawImage method to draw pageImage to the printer.
e.Graphics.DrawImage(pageImage, New
Point(e.MarginBounds.Left, e.MarginBounds.Top))
' Increment _PagePrinted.
_PagesPrinted += 1
' If all pages have been printed -
either during preview or print - set
' the PrintPageEventsArgs HasMorePages
property to false to stop printing.
If Me._PagesPrinted
= (Me._pageRectangles.GetLength(0))
Then
e.HasMorePages = False
' Reset _PagesPrinted to 0
for the next print operation.
Me._PagesPrinted = 0
Else
' If not all pages have
been printed set the PrintPageEventArgs HasMorePages
' property to True so the
next page will be printed.
e.HasMorePages = True
End If
End
Sub
For more information visit the links below:
PrintDocument Class
PrinterSettings Class
PrintDialog Class
Click the link above to download Visual Basic source code in a Visual Studio 2005 solution which demonstrates how to
print a multiple page image.
Get Dot Net Code is a web site full of free and pay for use Visual Basic source code for Visual Basic programmers.
|