VintaSoft Imaging .NET SDK 12.3: Documentation for .NET developer
In This Topic
    Print images in WPF
    In This Topic
    The WpfImagePrintManager class is used for printing and print previewing of images in WPF.
    By default the WpfImagePrintManager class prints the entire image without scaling, image is divided into pages if the entire image cannot be printed on a single page.
    Also class allows to scale an image during printing, scaling mode can be chosen using the WpfImagePrintManager.PrintScaleMode property.
    The following image scaling modes are supported:


    Before print start, margins for all pages selected for printing can be defined using WpfImagePrintManager.PagePadding property.

    During print process, margins for the page which is being printed can be defined using WpfImagePrintManager.PagePadding property.

    Padding for image being printed on the page can be defined using WpfImagePrintManager.ImagePadding property.

    The image can be centered on a page while printing using WpfImagePrintManager.Center property.


    The WpfImagePrintManager class uses a standard WPF settings dialog, which is implemented in System.Windows.Controls.PrintDialog class, for choosing a print device and adjusting printing parameters.


    While printing WpfImagePrintManager class raises WpfImagePrintManager.PrintingProgress event, which allows to obtain general information about print progress and cancel printing if necessary.
    Also while printing WpfImagePrintManager class raises a set of events, which allow to obtain detailed information about print process and control of all printing aspects:

    Here is an example that shows how to print an image collection with default print settings, each image from the collection will be printed on separate page, each image will be scaled up on page in BestFit mode:
    /// <summary>
    /// Prints images on default printer.
    /// </summary>
    /// <param name="images">The images, which must be printed.</param>
    public void PrintImagesOnDefaultPrinter(Vintasoft.Imaging.ImageCollection images)
    {
        // create print manager
        Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
            new Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager();
        // specify image collection which should be printed
        imagePrintManager.Images = images;
        // specify that each image must be resized to fit within the page margins,
        // image proportions is not changed
        imagePrintManager.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit;
    
        // subscribe to the PrintingProgress event
        imagePrintManager.PrintingProgress +=
            new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(imagePrintManager_PrintingProgress);
    
        // start the printing process
        imagePrintManager.Print("Print task title");
    }
    
    /// <summary>
    /// Image printing is in progress.
    /// </summary>
    private void imagePrintManager_PrintingProgress(object sender, Vintasoft.Imaging.ProgressEventArgs e)
    {
        // if printing is started
        if (e.Progress == 0)
            System.Console.Write("Printing: ");
    
        // write the printing progress
        System.Console.Write(string.Format("{0} ", e.Progress));
    
        // if printing is finished
        if (e.Progress == 100)
        {
            Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
                (Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)sender;
            // unsubscribe from the PrintingProgress event
            imagePrintManager.PrintingProgress -=
                new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(imagePrintManager_PrintingProgress);
        }
    }
    
    ''' <summary>
    ''' Prints images on default printer.
    ''' </summary>
    ''' <param name="images">The images, which must be printed.</param>
    Public Sub PrintImagesOnDefaultPrinter(images As Vintasoft.Imaging.ImageCollection)
        ' create print manager
        Dim imagePrintManager As New Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager()
        ' specify image collection which should be printed
        imagePrintManager.Images = images
        ' specify that each image must be resized to fit within the page margins,
        ' image proportions is not changed
        imagePrintManager.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit
    
        ' subscribe to the PrintingProgress event
        AddHandler imagePrintManager.PrintingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf imagePrintManager_PrintingProgress)
    
        ' start the printing process
        imagePrintManager.Print("Print task title")
    End Sub
    
    ''' <summary>
    ''' Image printing is in progress.
    ''' </summary>
    Private Sub imagePrintManager_PrintingProgress(sender As Object, e As Vintasoft.Imaging.ProgressEventArgs)
        ' if printing is started
        If e.Progress = 0 Then
            System.Console.Write("Printing: ")
        End If
    
        ' write the printing progress
        System.Console.Write(String.Format("{0} ", e.Progress))
    
        ' if printing is finished
        If e.Progress = 100 Then
            Dim imagePrintManager As Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager = DirectCast(sender, Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)
            ' unsubscribe from the PrintingProgress event
            RemoveHandler imagePrintManager.PrintingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf imagePrintManager_PrintingProgress)
        End If
    End Sub
    



    Here is an example that shows how to choose a printer, run printing of image collection and cancel printing after the first half of image collection has been printed:
    /// <summary>
    /// Starts image printing and cancels the printing when printing progress is more than 50%.
    /// </summary>
    /// <param name="images">The images, which must be printed.</param>
    public void PrintImagesAndCancel(Vintasoft.Imaging.ImageCollection images)
    {
        // create print manager
        Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
            new Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager();
        // specify image collection which should be printed
        imagePrintManager.Images = images;
        // specify that each image must be resized to fit within the page margins,
        // image proportions is not changed
        imagePrintManager.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit;
    
        // create a print dialog
        System.Windows.Controls.PrintDialog printDialog = imagePrintManager.PrintDialog;
        printDialog.MinPage = 1;
        printDialog.MaxPage = (uint)imagePrintManager.Images.Count;
        printDialog.UserPageRangeEnabled = true;
    
        // show the print dialog
        if (printDialog.ShowDialog() == true)
        {
            // subscribe to the PrintingProgress event
            imagePrintManager.PrintingProgress +=
                new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(imagePrintManager_PrintingProgress);
    
            // start the printing process
            imagePrintManager.Print("Print task title");
        }
    }
    
    /// <summary>
    /// Image printing is in progress.
    /// </summary>
    private void imagePrintManager_PrintingProgress(object sender, Vintasoft.Imaging.ProgressEventArgs e)
    {
        // if printing is started
        if (e.Progress == 0)
            System.Console.Write("Printing: ");
    
        // write the printing progress
        System.Console.Write(string.Format("{0} ", e.Progress));
    
        // if printing progress is more than 50%
        if (e.Progress > 50)
        {
            if (e.CanCancel)
                e.Cancel = true;
    
            Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
                (Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)sender;
            // unsubscribe from the PrintingProgress event
            imagePrintManager.PrintingProgress -=
                new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(imagePrintManager_PrintingProgress);
        }
    }
    
    ''' <summary>
    ''' Starts image printing and cancels the printing when printing progress is more than 50%.
    ''' </summary>
    ''' <param name="images">The images, which must be printed.</param>
    Public Sub PrintImagesAndCancel(images As Vintasoft.Imaging.ImageCollection)
        ' create print manager
        Dim imagePrintManager As New Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager()
        ' specify image collection which should be printed
        imagePrintManager.Images = images
        ' specify that each image must be resized to fit within the page margins,
        ' image proportions is not changed
        imagePrintManager.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.BestFit
    
        ' create a print dialog
        Dim printDialog As System.Windows.Controls.PrintDialog = imagePrintManager.PrintDialog
        printDialog.MinPage = 1
        printDialog.MaxPage = CUInt(imagePrintManager.Images.Count)
        printDialog.UserPageRangeEnabled = True
    
        ' show the print dialog
        If printDialog.ShowDialog() = True Then
            ' subscribe to the PrintingProgress event
            AddHandler imagePrintManager.PrintingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf imagePrintManager_PrintingProgress)
    
            ' start the printing process
            imagePrintManager.Print("Print task title")
        End If
    End Sub
    
    ''' <summary>
    ''' Image printing is in progress.
    ''' </summary>
    Private Sub imagePrintManager_PrintingProgress(sender As Object, e As Vintasoft.Imaging.ProgressEventArgs)
        ' if printing is started
        If e.Progress = 0 Then
            System.Console.Write("Printing: ")
        End If
    
        ' write the printing progress
        System.Console.Write(String.Format("{0} ", e.Progress))
    
        ' if printing progress is more than 50%
        If e.Progress > 50 Then
            If e.CanCancel Then
                e.Cancel = True
            End If
    
            Dim imagePrintManager As Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager = DirectCast(sender, Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)
            ' unsubscribe from the PrintingProgress event
            RemoveHandler imagePrintManager.PrintingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf imagePrintManager_PrintingProgress)
        End If
    End Sub
    



    Here is an example that shows how to print all images from an image collection except the second one:
    /// <summary>
    /// Starts image printing and suppress the printing of second image.
    /// </summary>
    /// <param name="images">The images, which must be printed.</param>
    public void PrintImagesAndSuppressSecondImage(Vintasoft.Imaging.ImageCollection images)
    {
        // create print manager
        Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
            new Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager();
        // specify image collection which should be printed
        imagePrintManager.Images = images;
    
        // subscribe to the PrintingProgress event
        imagePrintManager.PrintingProgress +=
            new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(imagePrintManager_PrintingProgress);
        // subscribe to the ImagePrinting event
        imagePrintManager.ImagePrinting +=
            new System.EventHandler<Vintasoft.Imaging.Wpf.Print.WpfImagePrintingEventArgs>(imagePrintManager_ImagePrinting);
    
        // start the printing process
        imagePrintManager.Print("Print task title");
    }
    
    /// <summary>
    /// Image printing is started.
    /// </summary>
    private void imagePrintManager_ImagePrinting(object sender, Vintasoft.Imaging.Wpf.Print.WpfImagePrintingEventArgs e)
    {
        Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
            (Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)sender;
        // if printing of second image is starting
        if (e.Image == imagePrintManager.Images[1])
        {
            // unsubscribe from the ImagePrinting event
            imagePrintManager.ImagePrinting +=
                new System.EventHandler<Vintasoft.Imaging.Wpf.Print.WpfImagePrintingEventArgs>(imagePrintManager_ImagePrinting);
    
            // suppress printing of this image
            e.Suppress = true;
        }
    }
    
    /// <summary>
    /// Image printing is in progress.
    /// </summary>
    private void imagePrintManager_PrintingProgress(object sender, Vintasoft.Imaging.ProgressEventArgs e)
    {
        // if printing is started
        if (e.Progress == 0)
            System.Console.Write("Printing: ");
    
        // write the printing progress
        System.Console.Write(string.Format("{0} ", e.Progress));
    
        // if printing is finished
        if (e.Progress == 100)
        {
            Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager =
                (Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)sender;
            // unsubscribe from the PrintingProgress event
            imagePrintManager.PrintingProgress -=
                new System.EventHandler<Vintasoft.Imaging.ProgressEventArgs>(imagePrintManager_PrintingProgress);
        }
    }
    
    ''' <summary>
    ''' Starts image printing and suppress the printing of second image.
    ''' </summary>
    ''' <param name="images">The images, which must be printed.</param>
    Public Sub PrintImagesAndSuppressSecondImage(images As Vintasoft.Imaging.ImageCollection)
        ' create print manager
        Dim imagePrintManager As New Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager()
        ' specify image collection which should be printed
        imagePrintManager.Images = images
    
        ' subscribe to the PrintingProgress event
        AddHandler imagePrintManager.PrintingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf imagePrintManager_PrintingProgress)
        ' subscribe to the ImagePrinting event
        AddHandler imagePrintManager.ImagePrinting, New System.EventHandler(Of Vintasoft.Imaging.Wpf.Print.WpfImagePrintingEventArgs)(AddressOf imagePrintManager_ImagePrinting)
    
        ' start the printing process
        imagePrintManager.Print("Print task title")
    End Sub
    
    ''' <summary>
    ''' Image printing is started.
    ''' </summary>
    Private Sub imagePrintManager_ImagePrinting(sender As Object, e As Vintasoft.Imaging.Wpf.Print.WpfImagePrintingEventArgs)
        Dim imagePrintManager As Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager = DirectCast(sender, Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)
        ' if printing of second image is starting
        If e.Image Is imagePrintManager.Images(1) Then
            ' unsubscribe from the ImagePrinting event
            AddHandler imagePrintManager.ImagePrinting, New System.EventHandler(Of Vintasoft.Imaging.Wpf.Print.WpfImagePrintingEventArgs)(AddressOf imagePrintManager_ImagePrinting)
    
            ' suppress printing of this image
            e.Suppress = True
        End If
    End Sub
    
    ''' <summary>
    ''' Image printing is in progress.
    ''' </summary>
    Private Sub imagePrintManager_PrintingProgress(sender As Object, e As Vintasoft.Imaging.ProgressEventArgs)
        ' if printing is started
        If e.Progress = 0 Then
            System.Console.Write("Printing: ")
        End If
    
        ' write the printing progress
        System.Console.Write(String.Format("{0} ", e.Progress))
    
        ' if printing is finished
        If e.Progress = 100 Then
            Dim imagePrintManager As Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager = DirectCast(sender, Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager)
            ' unsubscribe from the PrintingProgress event
            RemoveHandler imagePrintManager.PrintingProgress, New System.EventHandler(Of Vintasoft.Imaging.ProgressEventArgs)(AddressOf imagePrintManager_PrintingProgress)
        End If
    End Sub
    



    A code example, which shows how to add header and/or footer to the printing image or page, can be found in the WpfPrintDemo application, which is available in SDK distribution.


    Print preview of images

    WpfImagePrintManager class can use WpfThumbnailViewer class for print preview of image collection.

    During the preview WpfImagePrintManager class allows to:


    Here is an example that shows how to adjust print preview before printing image collection:
    public void PrintPreviewImages(
        Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager imagePrintManager,
        Vintasoft.Imaging.ImageCollection images,
        Vintasoft.Imaging.Wpf.UI.WpfThumbnailViewer printPreviewViewer)
    {
        // specify image collection which should be printed
        imagePrintManager.Images = images;
    
        // specify a viewer which should be used for print preview
        imagePrintManager.Preview = printPreviewViewer;
    
        // specify how many pages should be shown horizontally and vertically in preview
        imagePrintManager.PreviewColumnCount = 2;
        imagePrintManager.PreviewRowCount = 2;
    
        // specify settings for Mozaic mode of print preview
        imagePrintManager.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.Mosaic;
        imagePrintManager.MosaicColumnCount = 2;
        imagePrintManager.MosaicRowCount = 3;
        imagePrintManager.DistanceBetweenImages = new System.Windows.Size(10, 10);
    }
    
    Public Sub PrintPreviewImages(imagePrintManager As Vintasoft.Imaging.Wpf.Print.WpfImagePrintManager, images As Vintasoft.Imaging.ImageCollection, printPreviewViewer As Vintasoft.Imaging.Wpf.UI.WpfThumbnailViewer)
        ' specify image collection which should be printed
        imagePrintManager.Images = images
    
        ' specify a viewer which should be used for print preview
        imagePrintManager.Preview = printPreviewViewer
    
        ' specify how many pages should be shown horizontally and vertically in preview
        imagePrintManager.PreviewColumnCount = 2
        imagePrintManager.PreviewRowCount = 2
    
        ' specify settings for Mozaic mode of print preview
        imagePrintManager.PrintScaleMode = Vintasoft.Imaging.Print.PrintScaleMode.Mosaic
        imagePrintManager.MosaicColumnCount = 2
        imagePrintManager.MosaicRowCount = 3
        imagePrintManager.DistanceBetweenImages = New System.Windows.Size(10, 10)
    End Sub
    



    Good example that shows an implementation of image collection print preview is available in WpfPrintDemo application.


    Print images with annotations

    The WpfAnnotatedImagePrintManager class, which is derived from WpfImagePrintManager class, must be used for printing and print preview of images with annotations.
    The WpfAnnotatedImagePrintManager class prints annotations in vector form and this provides more efficient memory usage and greater performance than when rasterizing annotations onto an image clone before printing. The WpfAnnotatedImagePrintManager class will work as WpfImagePrintManager class if the WpfAnnotatedImagePrintManager.PrintAnnotations property value is set to false.