使用PrintDocument将图像打印到文件

时间:2018-12-11 08:37:30

标签: c# 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无法打开该图片,因为Photo Viewer不支持此文件格式,或者您没有Photo Viewer的最新更新。

enter image description here

1 个答案:

答案 0 :(得分:4)

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

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

将文件的外部更改为xps

PrintFileName = @"D:\f.xps"