重现步骤
1.从https://github.com/supershopping/OxyPlotPrintTest克隆存储库
1.运行应用程序
1.点击“添加数据”按钮
1.点击“打印”按钮,选择一台打印机打印在“信纸大小的纸”。
平台:WPF
.NET版本:.NET 7
预期行为:我的目标是打印WPF UI Datagrid“ReportGrid”或窗口“Main”。打印代码只是转换/重新调整UI以适应一个“字母”页面。Oxyplot应该调整大小并打印。
实际行为:一旦UI被打印,你可以看到UI右侧的按钮被打印,但Oxyplot是空白的,没有打印。我有另一个应用程序使用.NET Framework 4.8而不是.NET 7。使用相同的打印代码,UI打印工作正常。这似乎是使用.NET 7的Oxyplot库的兼容性问题,我不知道如何解决这个问题,或者有解决办法吗?
<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;
}
}
字符串
x1c 0d1x的数据
1条答案
按热度按时间llew8vvj1#
我做了大量的测试.原来打印问题是与Oxyplot 2.1.2版本.当我使用2.0版本的库,打印工作正常.NET框架4.8和.NET7.抱歉从我原来的帖子混淆.我希望这个信息可以帮助别人谁也碰到这个问题.