c# - 将 Canvas 打印到 XPS 不需要的缩放和切断

标签 c# wpf canvas printing xps

更新

在写回复之前,可以查看 SamTheDev 的回答及其评论。 SamTheDev 的回答解决了这两个问题,但我不明白,为什么它解决了第一个问题。


我遇到了以下 XPS 问题,需要您的帮助来解决这些问题。赏金事件正在进行中。

问题 1:WPF 中 10 厘米长的行在 XPS 中不是 10 厘米长

我正在画这条 10 厘米长的黑线: 10 cm long black line WPF

制作它的屏幕截图,将其粘贴到 Word 并打印它显示我,它真的有 10 厘米长:

10 cm long line after printing with word

但是当打印生成的 XPS 文件时,该行只有大约 9.7 厘米长:

Screenshot if generated XPS file

Print of generated XPS file

绘制黑线的代码如下。我假定分辨率为每英寸 96 点,我想我需要在创建 XPS 文件之前进行一些缩放,但我不知道该怎么做。

XAML:

<Window x:Class="MWEXps.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:MWEXps"
        mc:Ignorable="d"
        Title="Sample 1 - 10 cm long black line" Height="300" Width="700">
    <Canvas x:Name="canvas">
        <!-- will be filled by code behind -->        
    </Canvas>
</Window>

背后的代码: (基本上只有constructor和printCanvas方法相关)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // a 10 cm long line
        PathFigure lineFigure1 = new PathFigure();
        lineFigure1.StartPoint = cmPoint(3, 1);

        LineSegment lineSegment1 = new LineSegment();
        lineSegment1.Point = cmPoint(13, 1);
        lineFigure1.Segments.Add(lineSegment1);

        drawPathFigure(lineFigure1, canvas, Colors.Black);

        // save as XPS
        printCanvas();
    }


    public void printCanvas()
    {
        XpsDocument doc = new XpsDocument(@".\canvas-10cm.xps", System.IO.FileAccess.Write);
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

        writer.Write(canvas);
        doc.Close();
    }

    // helper creating a point in centimeters
    public Point cmPoint(double x, double y)
    {
        return new Point(cmToWpf(x), cmToWpf(y));
    }


    // helper converting a length in device independent pixels to centimeters
    public double wpfToCm(double wpfValue)
    {
        double factor = (96 / 2.54);
        return wpfValue / factor;
    }

    // helper converting a length in centimeters to device independent pixels
    public double cmToWpf(double cmValue)
    {
        double factor = (96 / 2.54);
        return cmValue * factor;
    }


    // helper for drawing a figure
    public void drawPathFigure(PathFigure figure, Canvas canvas, Color color)
    {
        PathGeometry pathGeometry = new PathGeometry();
        pathGeometry.Figures.Add(figure);


        Path path = new Path();
        path.Stretch = Stretch.None;
        path.StrokeLineJoin = PenLineJoin.Miter;
        path.Stroke = new SolidColorBrush(color);
        path.StrokeThickness = 1;

        path.Data = pathGeometry;
        canvas.Children.Add(path);
    }
}

问题 2:在创建的 XPS 上切断 100 厘米长的线

在第二个示例中,我绘制了一条 100 厘米长的红线。显然它长于窗口大小。这不是问题,问题是它在生成的 XPS 文件上也被切断了。

100 cm long red line

生成的 XPS 文件如下所示: 100 cm long red line on XPS

绘制红线的代码如下。可以看到,红线被简单地切断了。我需要 XPS 中完整的 100 厘米红线 - 我该怎么做?

XAML:

<Window x:Class="MWEXps.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:MWEXps"
        mc:Ignorable="d"
        Title="Sample 2 - 100 cm long red line" Height="300" Width="700">
    <Canvas x:Name="canvas">
        <!-- will be filled by code behind -->        
    </Canvas>
</Window>

背后的代码: (基本上只有constructor和printCanvas方法相关)

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // a 100 cm long line
        PathFigure lineFigure1 = new PathFigure();
        lineFigure1.StartPoint = cmPoint(3, 1);

        LineSegment lineSegment1 = new LineSegment();
        lineSegment1.Point = cmPoint(103, 1);
        lineFigure1.Segments.Add(lineSegment1);

        drawPathFigure(lineFigure1, canvas, Colors.Red);

        // save as XPS
        printCanvas();
    }


    public void printCanvas()
    {
        XpsDocument doc = new XpsDocument(@".\canvas-100cm.xps", System.IO.FileAccess.Write);
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);

        writer.Write(canvas);
        doc.Close();
    }

    // helper creating a point in centimeters
    public Point cmPoint(double x, double y)
    {
        return new Point(cmToWpf(x), cmToWpf(y));
    }


    // helper converting a length in device independent pixels to centimeters
    public double wpfToCm(double wpfValue)
    {
        double factor = (96 / 2.54);
        return wpfValue / factor;
    }

    // helper converting a length in centimeters to device independent pixels
    public double cmToWpf(double cmValue)
    {
        double factor = (96 / 2.54);
        return cmValue * factor;
    }


    // helper for drawing a figure
    public void drawPathFigure(PathFigure figure, Canvas canvas, Color color)
    {
        PathGeometry pathGeometry = new PathGeometry();
        pathGeometry.Figures.Add(figure);


        Path path = new Path();
        path.Stretch = Stretch.None;
        path.StrokeLineJoin = PenLineJoin.Miter;
        path.Stroke = new SolidColorBrush(color);
        path.StrokeThickness = 1;

        path.Data = pathGeometry;
        canvas.Children.Add(path);
    }
}

感谢您对此的支持!

最佳答案

快速提醒一下,像素大小取决于两个因素:显示分辨率显示器的物理尺寸,因此物理英寸和像素之间没有固定关系,WPF 使用逻辑英寸并通过转换:一个逻辑英寸是 96 像素 但这可以根据用户进行更改偏好。

所以“标尺”英寸或厘米在 wpf 上并不真正存在,只有从一个屏幕到另一个屏幕的合乎逻辑的。

例如,我的两个屏幕上黑线的大小:

enter image description here

enter image description here

  1. 关于红线,您可以使用 Arrange 根据其子项修复 Canvas 大小方法,将您的 PrintCanvas 方法更新为:

      public void printCanvas()
      {
         XpsDocument doc = new XpsDocument(@".\canvas.xps", System.IO.FileAccess.Write);
        XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
    
        // The size of the canvas
        System.Windows.Size size = new System.Windows.Size(cmToWpf(102), cmToWpf(6));
        // Arrange canvas           
        canvas.Arrange(new Rect(size));
    
    
    
        writer.Write(canvas);
        doc.Close();
    }
    

引用资料

DPI and Device-Independent Pixels

What measurement units does Silverlight and WPF use?

关于c# - 将 Canvas 打印到 XPS 不需要的缩放和切断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35245003/

相关文章:

c# - 如何声明 C# 匿名类型而不创建它的实例?

WPF - MVVM - NHibernate 验证

Java Swing - 闪烁的 Canvas 图形

javascript - Opera:在 JavaScript 中绘制椭圆形时出现问题

javascript - 创建 Canvas

c# - 使用 C# 的公钥验证 JWT

c# - 如果没有及时收到网关服务器的确认,可能需要通过Socket重新发送数据

c# - 如何处理 GridView 中的行双击?

c# - Webbrowser 禁用所有音频输出 - 从在线广播到 youtube

c# - WPF DocumentViewer - 无需确认即可打印