使用 PrintDocument 将图像打印到文件

Print Image to file using PrintDocument

我有 C# 项目(ASP.NET MVC 项目中的 ClassLibrary)

我想使用 PrintDocument

将图像 (System.Drawing.Image) 打印到文件
private static void SendToPrinter(Image barkod)
{
    PrintDocument pd = new PrintDocument();
    pd.PrinterSettings = new PrinterSettings
    {
        PrinterName = "Microsoft XPS Document Writer",
        PrintToFile = true,
        PrintFileName = @"D:\f.jpg"
    };

    pd.PrintPage += (o, e) => 
    {
        Point loc = new Point(100, 100);
        e.Graphics.DrawImage(barkod, loc);
    };
    pd.Print();
    barkod.Dispose();
}

发生的事情是文件是在特定位置创建的,但是当我尝试打开图像时出现错误

Windows Photo Viewer can't open this picture because either Photo Viewer doesn't support this file format, or you don't have the latest updates to Photo Viewer.

XPS Document Writer 以 *.xps 或 *.oxps 格式打印。

您需要考虑将 xps|oxps 转换为 .jpg

将文件扩展名改为xps

PrintFileName = @"D:\f.xps"