Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Print a Real Image Size in Inches

Take the following steps to start a project and to add some code that prints an image:

  1. Start Visual Studio .NET

  2. Choose File->New->Project... from the menu

  3. In the New Project dialog box, choose either "Visual C#" or "Visual Basic" in the Projects Type List, and choose "Windows Forms Application" in the Templates List

  4. Enter the project name as "PrintImageTutorial" in the Name field, and then click OK. If desired, type a new location for your project or select a directory using the Browse button, and then click OK

  5. In the "Solution Explorer" window, right-click on the project and select "Add Reference..." from the context menu. In the "Add Reference" dialog box, select the ".NET" tab and select the following components - If you used the LEADTOOLS Setup, the LEADTOOLS components will be visible under the .NET components and you should select them from the list, otherwise, select the "Browse" tab and browse to the LEADTOOLS setup folder – Default location is: C:\Program Files (x86)\LEAD Technologies\LEADTOOLS 16.5\Bin\Dotnet\Win32

    • Leadtools.dll
    • Leadtools.Codecs.dll
    • Leadtools.WinForms.dll
    • Leadtools.Codecs.Bmp.dll
    • Leadtools.Codecs.Cmp.dll
    • Leadtools.Codecs.Fax.dll
    • Leadtools.Codecs.Tif.dll

    The above references will allow you to use BMP, JPG and TIF files. If you require more file formats, please refer to the File Formats section in the Files To Be Included With Your Application topic

  6. Since we are going to use the Win32 (x86) assembly references, we must change the build target of this project to be x86. Select Project->PrintImageTutorial Properties from the main menu and in the project properties dialog, and follow the following steps:

    • For the C# project, select the Build tab, select "All Configurations" from the Configuration drop down selection box and select "x86" from the Platform target drop down selection box

    • For the VB project, select the Compile tab and select "All Configurations" from the Configuration drop down selection box. Click the Advanced Compile Options button and select "x86" from the Target CPU drop down selection box and click OK to close the dialog.

  7. Make sure Form1 is in design view. From the toolbox (View->Toolbox from the main menu), drag and drop a MenuStrip control to the form. Click on the new menuStrip1 control and add the following top-level menu items:

    Type Name Text Shortcut Keys
    ToolStripMenuItem fileToolStripMenuItem File
    ToolStripMenuItem pageToolStripMenuItem Page
  8. Select the "File" menu item and add the following sub items:

    Type Name Text Shortcut Keys
    ToolStripMenuItem openToolStripMenuItem Open... Ctrl+O
    ToolStripSeparator toolStripMenuItem1
    ToolStripMenuItem printPreviewToolStripMenuItem Print preview
    ToolStripMenuItem printSetupToolStripMenuItem Print setup...
    ToolStripMenuItem printToolStripMenuItem Print... Ctrl+P
    ToolStripSeparator toolStripMenuItem2
    ToolStripMenuItem exitToolStripMenuItem Exit
  9. Select the "Page" menu item and add the following sub items:

    Type Name Text Shortcut Keys
    ToolStripMenuItem firstPageToolStripMenuItem First page
    ToolStripMenuItem previousPageToolStripMenuItem Previous page
    ToolStripMenuItem nextPageToolStripMenuItem Next page
    ToolStripMenuItem lastPageToolStripMenuItem Last page
  10. Select the "Options" menu item and add the following sub items:

    Type Name Text Shortcut Keys
    ToolStripMenuItem usePageMarginsToolStripMenuItem Use page margins
    ToolStripMenuItem fitImageToPageToolStripMenuItem Fit image to page
  11. Go to the toolbox (View->Toolbox) and drag and drop an instance of LEADTOOLS RasterImageViewer control to the form (If you do not have RasterImageViewer in your toolbox, select Tools->Choose Toolbox Items from the main menu. Select the ".NET Framework Components" tab and select the "RasterImageViewer" component). After you dropped the new control to the form, change the following properties of "rasterImageViewer1" to the specified values:

    Property Value
    Dock Fill
    UseDpi True
  12. Switch to Form1 code view (right-click Form1 in the solution explorer then select View Code) and add the following lines at the beginning of the file:

    [Visual Basic]

    
    Imports System.Drawing.Printing
    Imports Leadtools
    Imports Leadtools.Codecs
    Imports Leadtools.WinForms
    

    [C#]

    
    using System.Drawing.Printing;
    using Leadtools;
    using Leadtools.Codecs;
    using Leadtools.WinForms;
    
  13. Add the following private variables to Form1:

    [Visual Basic]

    
    ' The RasterCodecs object to use when loading images
    Private _rasterCodecs As RasterCodecs
    ' The PrintDocument object we will be using throughout this demo
    Private _printDocument As PrintDocument
    ' The current page number bring printed
    Private _currentPrintPageNumber As Integer
    ' The current image file name
    Private _currentImageFileName As String
    

    [C#]

    
    // The RasterCodecs object to use when loading images
    private RasterCodecs _rasterCodecs;
    // The PrintDocument object we will be using throughout this demo
    private PrintDocument _printDocument;
    // The current page number bring printed
    private int _currentPrintPageNumber;
    // The current image file name
    private string _currentImageFileName;
    
  14. Add the following code to Form1:

    [Visual Basic]

    
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
       ' Optioanal: Unlock support for Document capabilities for ScaleToGray and Bicubic painting
       RasterSupport.Unlock(RasterSupportType.Document, "Your Document support unlock key here")
    
       ' Initialize the RasterCodecs object to use when loading image
       RasterCodecs.Startup()
       Me._rasterCodecs = New RasterCodecs()
    
    
       ' Check if we have any printers installed
       If IsNothing(PrinterSettings.InstalledPrinters) OrElse (PrinterSettings.InstalledPrinters.Count < 1) Then
          MessageBox.Show( _
             Me, _
             "No printer installed on this machine, this tutorial cannot continue", _
             "PrintImageTutorial", _
             MessageBoxButtons.OK, _
             MessageBoxIcon.Exclamation)
          Me.Close()
       Else
          ' Create the print document object we will be using
          Me._printDocument = New PrintDocument()
          ' Add handlers for the print events
          AddHandler Me._printDocument.BeginPrint, AddressOf _printDocument_BeginPrint
          AddHandler Me._printDocument.PrintPage, AddressOf _printDocument_PrintPage
          AddHandler Me._printDocument.EndPrint, AddressOf _printDocument_EndPrint
       End If
    
       MyBase.OnLoad(e)
    End Sub
    
    Protected Overrides Sub OnFormClosed(ByVal e As System.Windows.Forms.FormClosedEventArgs)
       ' Dispose the resources we are using
    
       If Not IsNothing(Me._printDocument) Then
          Me._printDocument.Dispose()
       End If
    
       If Not IsNothing(Me._rasterCodecs) Then
          Me._rasterCodecs.Dispose()
       End If
    
       RasterCodecs.Shutdown()
    
       MyBase.OnFormClosed(e)
    End Sub
    

    [C#]

    
    protected override void OnLoad(EventArgs e)
    {
       // Optioanal: Unlock support for Document capabilities for ScaleToGray and Bicubic painting
       RasterSupport.Unlock(RasterSupportType.Document, "Your Document support unlock key here");
    
       // Initialize the RasterCodecs object to use when loading image
       RasterCodecs.Startup();
       this._rasterCodecs = new RasterCodecs();
    
       // Check if we have any printers installed
       if(PrinterSettings.InstalledPrinters == null || PrinterSettings.InstalledPrinters.Count < 1)
       {
          MessageBox.Show(
             this,
             "No printer installed on this machine, this tutorial cannot continue",
             "PrintImageTutorial",
             MessageBoxButtons.OK,
             MessageBoxIcon.Exclamation);
          this.Close();
       }
       else
       {
          // Create the print document object we will be using
          this._printDocument = new PrintDocument();
          // Add handlers for the print events
          this._printDocument.BeginPrint += new PrintEventHandler(_printDocument_BeginPrint);
          this._printDocument.PrintPage += new PrintPageEventHandler(_printDocument_PrintPage);
          this._printDocument.EndPrint += new PrintEventHandler(_printDocument_EndPrint);
       }
    
       base.OnLoad(e);
    }
    
    protected override void OnFormClosed(FormClosedEventArgs e)
    {
       // Dispose the resources we are using
    
       if(this._printDocument != null)
       {
          this._printDocument.Dispose();
       }
    
       if(this._rasterCodecs != null)
       {
          this._rasterCodecs.Dispose();
       }
    
       RasterCodecs.Shutdown();
    
       base.OnFormClosed(e);
    }
    
  15. Add the following code to the DropDownOpening event of the fileToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub fileToolStripMenuItem_DropDownOpening(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fileToolStripMenuItem.DropDownOpening
       ' Update the UI state
    
       printPreviewToolStripMenuItem.Enabled = Not IsNothing(rasterImageViewer1.Image)
       printToolStripMenuItem.Enabled = Not IsNothing(rasterImageViewer1.Image)
    End Sub
    

    [C#]

    
    private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
    {
       // Update the UI state
    
       printPreviewToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null);
       printToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null);
    }
    
  16. Add the following code to the Click event of the openToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub openToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openToolStripMenuItem.Click
       ' Load an image into the viewer
       Using dlg As New OpenFileDialog()
          dlg.Filter = "All Files|*.*"
          If dlg.ShowDialog(Me) = DialogResult.OK Then
             Try
                ' Load all the pages in the file
                rasterImageViewer1.Image = Me._rasterCodecs.Load(dlg.FileName)
                Me._currentImageFileName = dlg.FileName
                UpdateCaption()
             Catch ex As Exception
                MessageBox.Show(Me, ex.Message, "PrintImageTutorial", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
             End Try
          End If
       End Using
    End Sub
    

    [C#]

    
    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Load an image into the viewer
       using(OpenFileDialog dlg = new OpenFileDialog())
       {
          dlg.Filter = "All Files|*.*";
          if(dlg.ShowDialog(this) == DialogResult.OK)
          {
             try
             {
                // Load all the pages in the file
                rasterImageViewer1.Image = this._rasterCodecs.Load(dlg.FileName);
                this._currentImageFileName = dlg.FileName;
                UpdateCaption();
             }
             catch(Exception ex)
             {
                MessageBox.Show(this, ex.Message, "PrintImageTutorial", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
             }
          }
       }
    }
    
  17. Add the following code to the Click event of the printPreviewToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub printPreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printPreviewToolStripMenuItem.Click
       ' Set the print document
       SetupPrintDocument()
    
       ' Use the .NET PrintPreviewDialog
       Using dlg As New PrintPreviewDialog()
          ' Show the dialog
          dlg.Document = Me._printDocument
          dlg.WindowState = FormWindowState.Maximized
          dlg.ShowDialog(Me)
       End Using
    End Sub
    

    [C#]

    
    private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Set the print document
       SetupPrintDocument();
    
       // Use the .NET PrintPreviewDialog
       using(PrintPreviewDialog dlg = new PrintPreviewDialog())
       {
          // Show the dialog
          dlg.Document = this._printDocument;
          dlg.WindowState = FormWindowState.Maximized;
          dlg.ShowDialog(this);
       }
    }
    
  18. Add the following code to the Click event of the printSetupToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub printSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printSetupToolStripMenuItem.Click
       ' Use the .NET PageSetupDialog
       Using dlg As New PageSetupDialog()
          dlg.Document = Me._printDocument
          dlg.ShowDialog(Me)
       End Using
    End Sub
    

    [C#]

    
    private void printSetupToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Use the .NET PageSetupDialog
       using(PageSetupDialog dlg = new PageSetupDialog())
       {
          dlg.Document = this._printDocument;
          dlg.ShowDialog(this);
       }
    }
    
  19. Add the following code to the Click event of the printToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub printToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printToolStripMenuItem.Click
       ' User might press the shortcut key, so we need to check
       ' if we are in a valid print state even though we disabled
       ' the menu item
       If IsNothing(rasterImageViewer1.Image) Then
          Return
       End If
    
       ' Directly print with showing the print preview dialog
    
       ' Set the print document
       SetupPrintDocument()
    
       Me._printDocument.Print()
    End Sub
    

    [C#]

    
    private void printToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // User might press the shortcut key, so we need to check
       // if we are in a valid print state even though we disabled
       // the menu item
       if(rasterImageViewer1.Image == null)
       {
          return;
       }
    
       // Directly print with showing the print preview dialog
    
       // Set the print document
       SetupPrintDocument();
    
       this._printDocument.Print();
    }
    
  20. Add the following code to the Click event of the exitToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub exitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitToolStripMenuItem.Click
       ' Close the application
       Close()
    End Sub
    

    [C#]

    
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Close the application
       Close();
    }
    
  21. Add the following code to the DropDownOpening event of the pageToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub pageToolStripMenuItem_DropDownOpening(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pageToolStripMenuItem.DropDownOpening
       ' Update the UI state
       firstPageToolStripMenuItem.Enabled = Not IsNothing(rasterImageViewer1.Image) AndAlso (rasterImageViewer1.Image.Page > 1)
       previousPageToolStripMenuItem.Enabled = firstPageToolStripMenuItem.Enabled
       nextPageToolStripMenuItem.Enabled = Not IsNothing(rasterImageViewer1.Image) AndAlso (rasterImageViewer1.Image.Page < rasterImageViewer1.Image.PageCount)
       lastPageToolStripMenuItem.Enabled = nextPageToolStripMenuItem.Enabled
    End Sub
    

    [C#]

    
    private void pageToolStripMenuItem_DropDownOpening(object sender, EventArgs e)
    {
       // Update the UI state
       firstPageToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null && rasterImageViewer1.Image.Page > 1);
       previousPageToolStripMenuItem.Enabled = firstPageToolStripMenuItem.Enabled;
       nextPageToolStripMenuItem.Enabled = (rasterImageViewer1.Image != null && rasterImageViewer1.Image.Page < rasterImageViewer1.Image.PageCount);
       lastPageToolStripMenuItem.Enabled = nextPageToolStripMenuItem.Enabled;
    }
    
  22. Add the following code to the Click event of the firstPageToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub firstPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles firstPageToolStripMenuItem.Click
       ' Go to the first page in the image
       rasterImageViewer1.Image.Page = 1
       UpdateCaption()
    End Sub
    

    [C#]

    
    private void firstPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Go to the first page in the image
       rasterImageViewer1.Image.Page = 1;
       UpdateCaption();
    }
    
  23. Add the following code to the Click event of the previousPageToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub previousPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previousPageToolStripMenuItem.Click
       ' Go to the previous page in the image
       rasterImageViewer1.Image.Page = rasterImageViewer1.Image.Page - 1
       UpdateCaption()
    End Sub
    

    [C#]

    
    private void previousPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Go to the previous page in the image
       rasterImageViewer1.Image.Page--;
       UpdateCaption();
    }
    
  24. Add the following code to the Click event of the nextPageToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub nextPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextPageToolStripMenuItem.Click
       ' Go to the next page in the image
       rasterImageViewer1.Image.Page = rasterImageViewer1.Image.Page + 1
       UpdateCaption()
    End Sub
    

    [C#]

    
    private void nextPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Go to the next page in the image
       rasterImageViewer1.Image.Page++;
       UpdateCaption();
    }
    
  25. Add the following code to the Click event of the lastPageToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub lastPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lastPageToolStripMenuItem.Click
       ' Go to the last page in the image
       rasterImageViewer1.Image.Page = rasterImageViewer1.Image.PageCount
       UpdateCaption()
    End Sub
    

    [C#]

    
    private void lastPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Go to the last page in the image
       rasterImageViewer1.Image.Page = rasterImageViewer1.Image.PageCount;
       UpdateCaption();
    }
    
  26. Add the following code to the Click event of the usePageMarginsToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub usePageMarginsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles usePageMarginsToolStripMenuItem.Click
       ' Toggle the option
       usePageMarginsToolStripMenuItem.Checked = Not usePageMarginsToolStripMenuItem.Checked
    End Sub
    

    [C#]

    
    private void usePageMarginsToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Toggle the option
       usePageMarginsToolStripMenuItem.Checked = !usePageMarginsToolStripMenuItem.Checked;
    }
    
  27. Add the following code to the Click event of the fitImageToPageToolStripMenuItem menu item:

    [Visual Basic]

    
    Private Sub fitImageToPageToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fitImageToPageToolStripMenuItem.Click
       ' Toggle the option
       fitImageToPageToolStripMenuItem.Checked = Not fitImageToPageToolStripMenuItem.Checked
    End Sub
    

    [C#]

    
    private void fitImageToPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
       // Toggle the option
       fitImageToPageToolStripMenuItem.Checked = !fitImageToPageToolStripMenuItem.Checked;
    }
    
  28. Add the following code to the Form1:

    [Visual Basic]

    
    Private Sub UpdateCaption()
       ' Update the caption of this demo to show the loaded image file name
       ' and current page number
       If Not IsNothing(rasterImageViewer1.Image) Then
          Text = String.Format( _
             "{0} - Page {1} (of {2}) - PrintImageTutorial", _
             Me._currentImageFileName, _
             rasterImageViewer1.Image.Page, _
             rasterImageViewer1.Image.PageCount)
       Else
          Text = "PrintImageTutorial"
       End If
    End Sub
    
    Private Sub SetupPrintDocument()
       ' Called before Print Preview or Print to setup the document
    
       ' Minimum/maximum is the number of pages in the image
       Me._printDocument.PrinterSettings.MinimumPage = 1
       Me._printDocument.PrinterSettings.MaximumPage = rasterImageViewer1.Image.PageCount
    
       ' Default to print all the pages
       Me._printDocument.PrinterSettings.FromPage = Me._printDocument.PrinterSettings.MinimumPage
       Me._printDocument.PrinterSettings.ToPage = Me._printDocument.PrinterSettings.MaximumPage
    
       ' Setup the document name
       Me._printDocument.DocumentName = Me._currentImageFileName
    End Sub
    
    Private Sub _printDocument_BeginPrint(ByVal sender As Object, ByVal e As PrintEventArgs)
       ' Reset the page number back to the first page
       Me._currentPrintPageNumber = 1
    End Sub
    
    Private Sub _printDocument_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs)
       ' Print a page here
    
       ' Get the print document object
       Dim document As PrintDocument = DirectCast(sender, PrintDocument)
    
       ' Create an new LEADTOOLS image printer class
       Dim printer As New RasterImagePrinter()
    
       ' Set the document object so page calculations can be performed
       printer.PrintDocument = document
    
       ' Check if we want to fit the image
       If fitImageToPageToolStripMenuItem.Checked Then
          ' Yes, fit and center the image into the maximum print area
          printer.SizeMode = RasterPaintSizeMode.FitAlways
          printer.HorizontalAlignMode = RasterPaintAlignMode.Center
          printer.VerticalAlignMode = RasterPaintAlignMode.Center
       Else
          ' No, print as normal (original size)
          printer.SizeMode = RasterPaintSizeMode.Normal
          printer.HorizontalAlignMode = RasterPaintAlignMode.Near
          printer.VerticalAlignMode = RasterPaintAlignMode.Near
       End If
    
       ' Account for FAX images that may have different horizontal and vertical resolution
       printer.UseDpi = True
    
       ' Print the whole image
       printer.ImageRectangle = Rectangle.Empty
    
       ' Use maximum page dimension, me will be equivalant of printing
       ' using Windows Photo Gallery
       printer.PageRectangle = RectangleF.Empty
       ' Inform the printer whether we want to ignore the page margins
       printer.UseMargins = usePageMarginsToolStripMenuItem.Checked
    
       ' Print the current page
       printer.Print(rasterImageViewer1.Image, Me._currentPrintPageNumber, e)
    
       ' Go to the next page
       Me._currentPrintPageNumber = Me._currentPrintPageNumber + 1
    
       ' Inform the printer whether we have more pages to print
       If Me._currentPrintPageNumber <= document.PrinterSettings.ToPage Then
          e.HasMorePages = True
       Else
          e.HasMorePages = False
       End If
    End Sub
    
    Private Sub _printDocument_EndPrint(ByVal sender As Object, ByVal e As PrintEventArgs)
       ' Nothing to do here in this tutorial
    End Sub
    

    [C#]

    
    private void UpdateCaption()
    {
       // Update the caption of this demo to show the loaded image file name
       // and current page number
       if(rasterImageViewer1.Image != null)
       {
          Text = string.Format(
             "{0} - Page {1} (of {2}) - PrintImageTutorial",
             this._currentImageFileName,
             rasterImageViewer1.Image.Page,
             rasterImageViewer1.Image.PageCount);
       }
       else
       {
          Text = "PrintImageTutorial";
       }
    }
    
    private void SetupPrintDocument()
    {
       // Called before Print Preview or Print to setup the document
    
       // Minimum/maximum is the number of pages in the image
       this._printDocument.PrinterSettings.MinimumPage = 1;
       this._printDocument.PrinterSettings.MaximumPage = rasterImageViewer1.Image.PageCount;
    
       // Default to print all the pages
       this._printDocument.PrinterSettings.FromPage = this._printDocument.PrinterSettings.MinimumPage;
       this._printDocument.PrinterSettings.ToPage = this._printDocument.PrinterSettings.MaximumPage;
    
       // Setup the document name
       this._printDocument.DocumentName = this._currentImageFileName;
    }
    
    private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
    {
       // Reset the page number back to the first page
       this._currentPrintPageNumber = 1;
    }
    
    private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
    {
       // Print a page here
    
       // Get the print document object
       PrintDocument document = sender as PrintDocument;
    
       // Create an new LEADTOOLS image printer class
       RasterImagePrinter printer = new RasterImagePrinter();
    
       // Set the document object so page calculations can be performed
       printer.PrintDocument = document;
    
       // Check if we want to fit the image
       if(fitImageToPageToolStripMenuItem.Checked)
       {
          // Yes, fit and center the image into the maximum print area
          printer.SizeMode = RasterPaintSizeMode.FitAlways;
          printer.HorizontalAlignMode = RasterPaintAlignMode.Center;
          printer.VerticalAlignMode = RasterPaintAlignMode.Center;
       }
       else
       {
          // No, print as normal (original size)
          printer.SizeMode = RasterPaintSizeMode.Normal;
          printer.HorizontalAlignMode = RasterPaintAlignMode.Near;
          printer.VerticalAlignMode = RasterPaintAlignMode.Near;
       }
    
       // Account for FAX images that may have different horizontal and vertical resolution
       printer.UseDpi = true;
    
       // Print the whole image
       printer.ImageRectangle = Rectangle.Empty;
    
       // Use maximum page dimension, this will be equivalant of printing
       // using Windows Photo Gallery
       printer.PageRectangle = RectangleF.Empty;
       // Inform the printer whether we want to ignore the page margins
       printer.UseMargins = usePageMarginsToolStripMenuItem.Checked;
    
       // Print the current page
       printer.Print(rasterImageViewer1.Image, this._currentPrintPageNumber, e);
    
       // Go to the next page
       this._currentPrintPageNumber++;
    
       // Inform the printer whether we have more pages to print
       if(this._currentPrintPageNumber <= document.PrinterSettings.ToPage)
       {
          e.HasMorePages = true;
       }
       else
       {
          e.HasMorePages = false;
       }
    }
    
    private void _printDocument_EndPrint(object sender, PrintEventArgs e)
    {
       // Nothing to do here in this tutorial
    }
    
  29. Build, and Run the program to test it.