ImageGear .NET v25.2 - Updated
Printing Images with Native Scaling
Developer Guide > How to Work with... > Common Operations > Printing > Printing in WPF Environment > Printing Images with Native Scaling

When using Native scaling, the application should re-render the ImageGear object before printing, using the printer resolution. You can obtain printer resolution, corresponding to user selected printer settings, using the properties of System.Printing.PrintTicket class. The example below demonstrates this approach:

C#
Copy Code
void SetupPrintResolution(System.Printing.PrintTicket printTicket, IImGearResolution resolution)
{
     if (printTicket.PageResolution != null && printTicket.PageResolution.X != null && printTicket.PageResolution.Y != null)
     {
          // Explicit resolution info is available.
          resolution.XNumerator = (int)printTicket.PageResolution.X.Value;
          resolution.YNumerator = (int)printTicket.PageResolution.Y.Value;
     }
     else
     {
          // Use qualitative resolution.
          System.Printing.PageQualitativeResolution qResolution;
          if (printTicket.PageResolution == null)
               qResolution = System.Printing.PageQualitativeResolution.Default;
          else
               qResolution = (System.Printing.PageQualitativeResolution)(
                    printTicket.PageResolution.QualitativeResolution ??
                    System.Printing.PageQualitativeResolution.Default);
          double defResolution;
   
          switch (qResolution)
          {
               case System.Printing.PageQualitativeResolution.Draft:
                    defResolution = 300;
                    break;
               case System.Printing.PageQualitativeResolution.Normal:
                    defResolution = 600;
                    break;
               case System.Printing.PageQualitativeResolution.High:
                    defResolution = 1200;
                    break;
               default:
                    defResolution = 600;
                    break;
          }
          resolution.XNumerator = (int)defResolution;
          resolution.YNumerator = (int)defResolution;
     }
     resolution.Units = ImGearResolutionUnits.INCHES;
}

ImageGear needs to know the bounds for the drawing. If the application uses a DrawToCanvas Method overload that accepts an IImGearScrollableView Interface, ImageGear uses its bounds. If the application uses an overload that does not accept an IImGearScrollableView Interface, which is the usual way for printing, the application shall set canvas Width and Height properties.

When the image has been drawn, the application should call the Measure and Arrange methods of the canvas that it uses for printing. This step is only necessary for printing, and not for display. It is important to call these methods after the DrawToCanvas Method and before sending the canvas to the system printing method, such as System.Windows.Controls.PrintDialog.PrintVisual().

The following example demonstrates the printing steps:

C#
Copy Code
void PrintWholeImageCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
     System.Windows.Controls.PrintDialog pDialog = new System.Windows.Controls.PrintDialog();
     if(pDialog.ShowDialog() == true)
     {
          // Set up scale settings.
          ImGearRenderSettings rs = new ImGearRenderSettings();

          // Use native scale settings. Provide specific resolution.
          ImGearNativeDisplayScaleSettings settings = new ImGearNativeDisplayScaleSettings();
          System.Printing.PrintTicket pt = pDialog.PrintTicket;
          SetupPrintResolution(pt, settings.Resolution);
          rs.ImageScaleSettings = settings;

          Canvas printCanvas = new Canvas();

          // Setup Display settings to print whole image.
          ImGearPresentationPageDisplay pageDisplay =
               new ImGearPresentationPageDisplay(imGearPageView.Display.Page,
               imGearPageView.Display.ARTPage);
          pageDisplay.Layout.FitMode = ImGearFitModes.FIT_TO_DEVICE;

          // printCanvas.Width, printCanvas.Height must be set before
          // pageDisplay.DrawToCanvas, when printing without a pageView.
          printCanvas.Width = pDialog.PrintableAreaWidth;
          printCanvas.Height = pDialog.PrintableAreaHeight;

          // Draw the image on the printCanvas.
          pageDisplay.DrawToCanvas(printCanvas, rs);

          // Measure and arrange canvas.
          printCanvas.Measure(new Size(pDialog.PrintableAreaWidth, pDialog.PrintableAreaHeight));
          printCanvas.Arrange(new Rect(printCanvas.DesiredSize));
       
          // Print the canvas.
          pDialog.PrintVisual(printCanvas, "Print ImageGear image");
     }
}
Is this page helpful?
Yes No
Thanks for your feedback.