Supports printing of an RasterImage
public class RasterImagePrinter Public Class RasterImagePrinter public ref class RasterImagePrinter The RasterImagePrinter provides properties and method to make the process of printing an RasterImage easier.
Printing using the .NET framework involves adding a handler to the System.Drawing.Printing.PrintDocument.PrintPage event. In that event handler, you setup a new instance of the RasterImagePrinter class, setup its properties as desired then call the Print method passing it the RasterImage to print, the page number to print and the System.Drawing.Printing.PrintPageEventArgs object obtained through your System.Drawing.Printing.PrintPageEventHandler.
Note: The RasterViewerCenterMode type has been renamed in version 15. Use the RasterPaintAlignMode enumeration instead.
using Leadtools;using Leadtools.Controls;using Leadtools.Codecs;using Leadtools.Drawing;using LeadtoolsExamples.Common;// The image we are printingprivate RasterImage myImage = null;// The current page number being printedprivate int currentPrintPageNumber;public void RasterImagePrinterExample(){// Check if there are printers installed on this machineif (PrinterSettings.InstalledPrinters == null || PrinterSettings.InstalledPrinters.Count < 1){MessageBox.Show("There are no printers installed on this machine");return;}// Load the imageusing (RasterCodecs codecs = new RasterCodecs()){this.myImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"));}// Create the print document objectusing (PrintDocument document = new PrintDocument()){// Setup the document pagesdocument.PrinterSettings.MinimumPage = 1;document.PrinterSettings.MaximumPage = this.myImage.PageCount;document.PrinterSettings.FromPage = 1;document.PrinterSettings.ToPage = this.myImage.PageCount;DialogResult result = DialogResult.OK;// Select the printerusing (PrintDialog printDlg = new PrintDialog()){printDlg.Document = document;printDlg.AllowSomePages = true;result = printDlg.ShowDialog();}// Setup the pageif (result == DialogResult.OK){using (PageSetupDialog pageSetupDlg = new PageSetupDialog()){pageSetupDlg.Document = document;pageSetupDlg.ShowDialog();}}if (result == DialogResult.OK){// Add handlers for Begin/Print and End print eventsdocument.BeginPrint += new PrintEventHandler(document_BeginPrint);document.PrintPage += new PrintPageEventHandler(document_PrintPage);document.EndPrint += new PrintEventHandler(document_EndPrint);// Use the .NET print preview dialogusing (PrintPreviewDialog printPreviewDlg = new PrintPreviewDialog()){printPreviewDlg.Document = document;printPreviewDlg.WindowState = FormWindowState.Maximized;result = printPreviewDlg.ShowDialog();}}}// Clean upthis.myImage.Dispose();}private void document_BeginPrint(object sender, PrintEventArgs e){// Reset the current page number// Since we are using the print preview dialog, this event will be called twice (once// to generate the print preview and once for actual printing). So, we must set this back// to the first print pagePrintDocument document = sender as PrintDocument;this.currentPrintPageNumber = document.PrinterSettings.FromPage;}private void document_EndPrint(object sender, PrintEventArgs e){// Nothing to do here}private void document_PrintPage(object sender, PrintPageEventArgs e){// Get the print document objectPrintDocument document = sender as PrintDocument;// Create an new LEADTOOLS image printer classRasterImagePrinter printer = new RasterImagePrinter();// Set the document object so page calculations can be performedprinter.PrintDocument = document;// We want to fit and center the image into the maximum print areaprinter.SizeMode = RasterPaintSizeMode.FitAlways;printer.HorizontalAlignMode = RasterPaintAlignMode.Center;printer.VerticalAlignMode = RasterPaintAlignMode.Center;// Account for FAX images that may have different horizontal and vertical resolutionprinter.UseDpi = true;// Print the whole imageprinter.ImageRectangle = Rectangle.Empty;// Use maximum page dimension ignoring the margins, this will be equivalant of printing// using Windows Photo Galleryprinter.PageRectangle = RectangleF.Empty;printer.UseMargins = false;// Print the current pageprinter.Print(this.myImage, this.currentPrintPageNumber, e);// Go to the next pagethis.currentPrintPageNumber++;// Inform the printer whether we have more pages to printif (this.currentPrintPageNumber <= document.PrinterSettings.ToPage)e.HasMorePages = true;elsee.HasMorePages = false;// De-couple our PrintDocument from the RasterImagePrinterprinter.PrintDocument = null;}static class LEAD_VARS{public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";}
Imports LeadtoolsImports Leadtools.ControlsImports Leadtools.CodecsImports Leadtools.Drawing' The image we are printingPrivate myImage As RasterImage = Nothing' The current page number being printedPrivate currentPrintPageNumber As Integer<TestMethod>Public Sub RasterImagePrinterExample()' Check if there are printers installed on this machineIf PrinterSettings.InstalledPrinters Is Nothing OrElse PrinterSettings.InstalledPrinters.Count < 1 ThenMessageBox.Show("There are no printers installed on this machine")ReturnEnd If' Load the imageUsing codecs As RasterCodecs = New RasterCodecs()Me.myImage = codecs.Load(Path.Combine(LEAD_VARS.ImagesDir, "Ocr1.tif"))End Using' Create the print document objectUsing document As PrintDocument = New PrintDocument()' Setup the document pagesdocument.PrinterSettings.MinimumPage = 1document.PrinterSettings.MaximumPage = Me.myImage.PageCountdocument.PrinterSettings.FromPage = 1document.PrinterSettings.ToPage = Me.myImage.PageCountDim result As DialogResult = System.Windows.Forms.DialogResult.OK' Select the printerUsing printDlg As PrintDialog = New PrintDialog()printDlg.Document = documentprintDlg.AllowSomePages = Trueresult = printDlg.ShowDialog()End Using' Setup the pageIf result = System.Windows.Forms.DialogResult.OK ThenUsing pageSetupDlg As PageSetupDialog = New PageSetupDialog()pageSetupDlg.Document = documentpageSetupDlg.ShowDialog()End UsingEnd IfIf result = System.Windows.Forms.DialogResult.OK Then' Add handlers for Begin/Print and End print eventsAddHandler document.BeginPrint, AddressOf document_BeginPrintAddHandler document.PrintPage, AddressOf document_PrintPageAddHandler document.EndPrint, AddressOf document_EndPrint' Use the .NET print preview dialogUsing printPreviewDlg As PrintPreviewDialog = New PrintPreviewDialog()printPreviewDlg.Document = documentprintPreviewDlg.WindowState = FormWindowState.Maximizedresult = printPreviewDlg.ShowDialog()End UsingEnd IfEnd Using' Clean upMe.myImage.Dispose()End SubPrivate Sub document_BeginPrint(ByVal sender As Object, ByVal e As PrintEventArgs)' Reset the current page number' Since we are using the print preview dialog, this event will be called twice (once' to generate the print preview and once for actual printing). So, we must set this back' to the first print pageDim document As PrintDocument = TryCast(sender, PrintDocument)Me.currentPrintPageNumber = document.PrinterSettings.FromPageEnd SubPrivate Sub document_EndPrint(ByVal sender As Object, ByVal e As PrintEventArgs)' Nothing to do hereEnd SubPrivate Sub document_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)' Get the print document objectDim document As PrintDocument = TryCast(sender, PrintDocument)' Create an new LEADTOOLS image printer classDim printer As RasterImagePrinter = New RasterImagePrinter()' Set the document object so page calculations can be performedprinter.PrintDocument = document' We want to fit and center the image into the maximum print areaprinter.SizeMode = RasterPaintSizeMode.FitAlwaysprinter.HorizontalAlignMode = RasterPaintAlignMode.Centerprinter.VerticalAlignMode = RasterPaintAlignMode.Center' Account for FAX images that may have different horizontal and vertical resolutionprinter.UseDpi = True' Print the whole imageprinter.ImageRectangle = Rectangle.Empty' Use maximum page dimension ignoring the margins, this will be equivalant of printing' using Windows Photo Galleryprinter.PageRectangle = RectangleF.Emptyprinter.UseMargins = False' Print the current pageprinter.Print(Me.myImage, Me.currentPrintPageNumber, e)' Go to the next pageMe.currentPrintPageNumber += 1' Inform the printer whether we have more pages to printIf Me.currentPrintPageNumber <= document.PrinterSettings.ToPage Thene.HasMorePages = TrueElsee.HasMorePages = FalseEnd If' De-couple our PrintDocument from the RasterImagePrinterprinter.PrintDocument = NothingEnd SubPublic NotInheritable Class LEAD_VARSPublic Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"End Class
|
Products |
Support |
Feedback: RasterImagePrinter Class - Leadtools.Controls |
Introduction |
Help Version 19.0.2017.6.20
|

Raster .NET | C API | C++ Class Library | JavaScript HTML5
Document .NET | C API | C++ Class Library | JavaScript HTML5
Medical .NET | C API | C++ Class Library | JavaScript HTML5
Medical Web Viewer .NET
Your email has been sent to support! Someone should be in touch! If your matter is urgent please come back into chat.
Chat Hours:
Monday - Friday, 8:30am to 6pm ET
Thank you for your feedback!
Please fill out the form again to start a new chat.
All agents are currently offline.
Chat Hours:
Monday - Friday
8:30AM - 6PM EST
To contact us please fill out this form and we will contact you via email.