The ImagePrintDocument control, which is derived from System.Drawing.Printing.PrintDocument control, is used for printing and print previewing of images in WinForms.
By default the ImagePrintDocument control prints the entire image without scaling, image is divided into pages if the entire image cannot be printed on a single page.
Also control allows to scale an image during printing, scaling mode can be chosen using the ImagePrintDocument.PrintScaleMode property.
The following image scaling modes are supported:
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging Private _printingImages As Vintasoft.Imaging.ImageCollection Private _printingImageIndex As Integer Public Sub PrintImagesOnDefaultPrinter(images As Vintasoft.Imaging.ImageCollection) ' save information about printing image collection in global variable _printingImages = images _printingImageIndex = 0 ' create print manager Dim imagePrintDocument As New Vintasoft.Imaging.Print.ImagePrintDocument() ' specify that each image must be resized to fit within the page margins, ' image proportions are not changed imagePrintDocument.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit ' subscribe to the PrintImage event AddHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf imagePrintDocument_PrintImage) ' start print imagePrintDocument.Print() End Sub Private Sub imagePrintDocument_PrintImage(sender As Object, e As Vintasoft.Imaging.Print.PrintImageEventArgs) e.Image = _printingImages(_printingImageIndex) _printingImageIndex += 1 If _printingImageIndex >= _printingImages.Count Then Dim imagePrintDocument As Vintasoft.Imaging.Print.ImagePrintDocument = DirectCast(sender, Vintasoft.Imaging.Print.ImagePrintDocument) ' unsubscribe from the PrintImage event RemoveHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf imagePrintDocument_PrintImage) ' indicate that there is no more images to print e.HasMoreImages = False Else ' indicate that there are additional images to print e.HasMoreImages = True End If End Sub
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging Vintasoft.Imaging.ImageCollection _printingImages; int _printingImageIndex; public void PrintImagesOnDefaultPrinter(Vintasoft.Imaging.ImageCollection images) { // save information about printing image collection in global variable _printingImages = images; _printingImageIndex = 0; // create print manager Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument = new Vintasoft.Imaging.Print.ImagePrintDocument(); // specify that each image must be resized to fit within the page margins, // image proportions are not changed imagePrintDocument.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit; // subscribe to the PrintImage event imagePrintDocument.PrintImage += new System.EventHandlerPrintImageEventArgs>(imagePrintDocument_PrintImage); // start print imagePrintDocument.Print(); } private void imagePrintDocument_PrintImage(object sender, Vintasoft.Imaging.Print.PrintImageEventArgs e) { e.Image = _printingImages[_printingImageIndex]; _printingImageIndex++; if (_printingImageIndex >= _printingImages.Count) { Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument = (Vintasoft.Imaging.Print.ImagePrintDocument)sender; // unsubscribe from the PrintImage event imagePrintDocument.PrintImage -= new System.EventHandlerPrintImageEventArgs>(imagePrintDocument_PrintImage); // indicate that there is no more images to print e.HasMoreImages = false; } else { // indicate that there are additional images to print e.HasMoreImages = true; } }
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging Private _printingImages As Vintasoft.Imaging.ImageCollection Private _printingImageIndex As Integer Public Sub PrintImagesAndCancel(images As Vintasoft.Imaging.ImageCollection) ' save information about printing image collection in global variable _printingImages = images _printingImageIndex = 0 ' create print manager Dim imagePrintDocument As New Vintasoft.Imaging.Print.ImagePrintDocument() ' specify that each image must be resized to fit within the page margins, ' image proportions is not changed imagePrintDocument.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit ' create an instance of PrintDialog class Dim printDialog1 As New System.Windows.Forms.PrintDialog() ' specify that printer settings should be obtain for imagePrintDocument printDialog1.Document = imagePrintDocument ' if printer is not selected If printDialog1.ShowDialog() <> System.Windows.Forms.DialogResult.OK Then ' exit Return End If ' subscribe to the PrintImage event AddHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf imagePrintDocument_PrintImageAndCancel) ' start print imagePrintDocument.Print() End Sub Private Sub imagePrintDocument_PrintImageAndCancel(sender As Object, e As Vintasoft.Imaging.Print.PrintImageEventArgs) If (_printingImageIndex >= _printingImages.Count) OrElse (_printingImageIndex >= (_printingImages.Count \ 2)) Then Dim imagePrintDocument As Vintasoft.Imaging.Print.ImagePrintDocument = DirectCast(sender, Vintasoft.Imaging.Print.ImagePrintDocument) ' unsubscribe from the PrintImage event RemoveHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf imagePrintDocument_PrintImageAndCancel) ' indicate that there is no more images to print e.HasMoreImages = False Return End If e.Image = _printingImages(_printingImageIndex) _printingImageIndex += 1 End Sub
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging Vintasoft.Imaging.ImageCollection _printingImages; int _printingImageIndex; public void PrintImagesAndCancel(Vintasoft.Imaging.ImageCollection images) { // save information about printing image collection in global variable _printingImages = images; _printingImageIndex = 0; // create print manager Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument = new Vintasoft.Imaging.Print.ImagePrintDocument(); // specify that each image must be resized to fit within the page margins, // image proportions is not changed imagePrintDocument.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit; // create an instance of PrintDialog class System.Windows.Forms.PrintDialog printDialog1 = new System.Windows.Forms.PrintDialog(); // specify that printer settings should be obtain for imagePrintDocument printDialog1.Document = imagePrintDocument; // if printer is not selected if (printDialog1.ShowDialog() != System.Windows.Forms.DialogResult.OK) // exit return; // subscribe to the PrintImage event imagePrintDocument.PrintImage += new System.EventHandlerPrintImageEventArgs>(imagePrintDocument_PrintImageAndCancel); // start print imagePrintDocument.Print(); } private void imagePrintDocument_PrintImageAndCancel(object sender, Vintasoft.Imaging.Print.PrintImageEventArgs e) { if ((_printingImageIndex >= _printingImages.Count) || (_printingImageIndex >= (_printingImages.Count / 2))) { Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument = (Vintasoft.Imaging.Print.ImagePrintDocument)sender; // unsubscribe from the PrintImage event imagePrintDocument.PrintImage -= new System.EventHandlerPrintImageEventArgs>(imagePrintDocument_PrintImageAndCancel); // indicate that there is no more images to print e.HasMoreImages = false; return; } e.Image = _printingImages[_printingImageIndex]; _printingImageIndex++; }
' The project, which uses this code, must have references to the following assemblies: ' - Vintasoft.Imaging Private _printingImages As Vintasoft.Imaging.ImageCollection Private _printingImageIndex As Integer Public Sub PrintImagesAndSuppressSecondImage(images As Vintasoft.Imaging.ImageCollection) ' save information about printing image collection in global variable _printingImages = images _printingImageIndex = 0 ' create print manager Dim imagePrintDocument As New Vintasoft.Imaging.Print.ImagePrintDocument() ' subscribe to the PrintImage event AddHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf imagePrintDocument_PrintImageAndSuppress) ' start print imagePrintDocument.Print() End Sub Private Sub imagePrintDocument_PrintImageAndSuppress(sender As Object, e As Vintasoft.Imaging.Print.PrintImageEventArgs) If _printingImageIndex = 1 Then _printingImageIndex += 1 End If If _printingImageIndex >= _printingImages.Count Then Dim imagePrintDocument As Vintasoft.Imaging.Print.ImagePrintDocument = DirectCast(sender, Vintasoft.Imaging.Print.ImagePrintDocument) ' unsubscribe from the PrintImage event RemoveHandler imagePrintDocument.PrintImage, New System.EventHandler(Of Vintasoft.Imaging.Print.PrintImageEventArgs)(AddressOf imagePrintDocument_PrintImageAndSuppress) ' indicate that there is no more images to print e.HasMoreImages = False Return End If e.Image = _printingImages(_printingImageIndex) _printingImageIndex += 1 End Sub
// The project, which uses this code, must have references to the following assemblies: // - Vintasoft.Imaging Vintasoft.Imaging.ImageCollection _printingImages; int _printingImageIndex; public void PrintImagesAndSuppressSecondImage(Vintasoft.Imaging.ImageCollection images) { // save information about printing image collection in global variable _printingImages = images; _printingImageIndex = 0; // create print manager Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument = new Vintasoft.Imaging.Print.ImagePrintDocument(); // subscribe to the PrintImage event imagePrintDocument.PrintImage += new System.EventHandlerPrintImageEventArgs>(imagePrintDocument_PrintImageAndSuppress); // start print imagePrintDocument.Print(); } private void imagePrintDocument_PrintImageAndSuppress(object sender, Vintasoft.Imaging.Print.PrintImageEventArgs e) { if (_printingImageIndex == 1) _printingImageIndex++; if (_printingImageIndex >= _printingImages.Count) { Vintasoft.Imaging.Print.ImagePrintDocument imagePrintDocument = (Vintasoft.Imaging.Print.ImagePrintDocument)sender; // unsubscribe from the PrintImage event imagePrintDocument.PrintImage -= new System.EventHandlerPrintImageEventArgs>(imagePrintDocument_PrintImageAndSuppress); // indicate that there is no more images to print e.HasMoreImages = false; return; } e.Image = _printingImages[_printingImageIndex]; _printingImageIndex++; }