如何打印高分辨率的c#/WPF图像

问题描述 投票:0回答:1

WPF/C# 打印的分辨率仅为 96dpi。我如何打印位图,例如200dpi? 这是我的伪代码:

var document = new PrintDocument
{
    PrintSettings = _settings,
    PageCount = 1
}

document.PrintPage += (sender, e) =>
{
    Debug.WriteLine("print pageSize " + e.pageSize + " "+ e.Graphics .DPI + "dpi");
    ...
    var rect1= new Rectangle(0,0,e.pageSize.Width,e.pageSize.Height);
    e.Graphics.DrawImage(image1, rect1);
}

document.Print();

我的问题是,e.pageSize 对于 A4 始终为 793x1122,e.Graphics.DPI 为 96。但我希望它更大,这样较大的 image1 就不会损失质量。

c# wpf printing
1个回答
0
投票

解决办法其实很简单。只是为了放大视觉效果,以获得更多像素来渲染。dpi 仍为 96,但我可以忽略这一点。 问题是,PageSize 也增加了。 但我在 DocumentPaginator 类中找到了另一个 DocumentPage 构造函数,用于指定具有更大视觉效果的原始页面大小,就像这样它可以工作。

using swd = System.Windows.Documents;

public override swd.DocumentPage GetPage(int pageNumber)
{
    var factor = 3f;
    
    var page = new Canvas
    {
        Handler = Handler,
        PageNumber = pageNumber,
        Width = ImageableArea.Width * factor,
        Height = ImageableArea.Height * factor,
        LayoutTransform = new swm.ScaleTransform(1 / factor, 1 / factor)   // this is scaling the output on the paper
    };

    var rect = new sw.Rect(new sw.Point(ImageableArea.X, ImageableArea.Y), ImageableArea.Size);
    page.Measure(ImageableArea.Size);
    page.Arrange(rect);
    page.UpdateLayout();

    // 17.12.24 VERY IMPORTANT, if we scale the canvas by factor, we would also get a bigger pagesize
    // so we need to specify the original pagesize in the ctor here:
    var docpage = new swd.DocumentPage(page, ImageableArea.Size, rect, rect);
    return docpage;
}
© www.soinside.com 2019 - 2024. All rights reserved.