Oxyplot is not printed (blank) when Printing WPF UI which is rescaled to fit on one page using .NET 7

74 Views Asked by At

Steps to reproduce

  1. Clone the repo from : https://github.com/supershopping/OxyPlotPrintTest
  2. Run application
  3. Click "Add Data" button
  4. Click "Print" button and select a printer to print on a "letter size paper".

Platform: WPF

.NET version: .NET 7

Expected behaviour: My goal is to print the WPF UI Datagrid "ReportGrid" or the Window "Main". The printing code is just to transform/resacle the UI to fit on one "letter" page. The Oxyplot is supposed to resize and print.

Actual behaviour: Once the UI is printed, you can see the buttons on the right side of the UI are prined but the Oxyplot is blank and not printed. I have another application using .NET framework 4.8 instead of .NET 7. With the same printing code, the UI printing is working properly. It seems an compatability issue with Oxyplot library using .NET 7, and I am not sure how to fix this or is there a workaround?

<Window x:Class="OxyPlotPrintTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:OxyPlotPrintTest"
    xmlns:oxy="http://oxyplot.org/wpf"
    Title="MainWindow" Height="800" Width="1280"
    Name="Main">

<Grid Name="ReportGrid">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="5*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <oxy:PlotView x:Name="plot"/>

    <StackPanel Grid.Column="1">
        <Button  Content="Add Data" Click="Button_Click_1" Height="150"/>
        <Button Content="Print" Click="Button_Click" Height="150"/>
    </StackPanel>

</Grid>
</Window>




 public partial class MainWindow : Window
{

 public PlotModel MyModel { get; private set; }

 public MainWindow()
 {
     MyModel = new PlotModel { Title = "Example 1" };
     MyModel.Series.Add(new FunctionSeries(Math.Cos, 0, 10, 0.1, "cos(x)"));
     
 }

 private void Button_Click(object sender, RoutedEventArgs e)
 {
     Visual visual = ReportGrid as Visual;
     Print(visual);
 }

 private void Print(Visual v)
 {

     System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement;

     if (e == null)
         return;

     PrintDialog pd = new PrintDialog();
     pd.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
     if (pd.ShowDialog() == true)
     {
         //store original scale
         Transform originalScale = e.LayoutTransform;
         //get selected printer capabilities
         System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);

         //get scale of the print wrt to screen of WPF visual
         double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / e.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
                        e.ActualHeight);

         //Transform the Visual to scale
         e.LayoutTransform = new ScaleTransform(scale, scale);

         //get the size of the printer page
         System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);

         //update the layout of the visual to the printer page size.
         e.Measure(sz);
         e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));

         //now print the visual to printer to fit on the one page.
         pd.PrintVisual(v, "My Print");

         //apply the original transform.
         e.LayoutTransform = originalScale;
     }
 }

 private void Button_Click_1(object sender, RoutedEventArgs e)
 {
     plot.Model = MyModel;
 }
}

enter image description here

1

There are 1 best solutions below

0
Owen Lee On

I did a lot of testing. It turns out the pringting issue is related to Oxyplot 2.1.2 version. When I am using 2.0 version of the library, the printing is working fine for both .NET framework 4.8 and .NET7. Sorry for the confusion from my original post. I hope this info can help someone who also bumps into this issue.