简体   繁体   中英

Print Image to file using PrintDocument

I have C# project (ClassLibrary in ASP.NET MVC project)

I want to print an Image (System.Drawing.Image) to file using PrintDocument

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();
}

What is happening is the file is created on the specific location, but when I try to open the image I get error

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 prints in *.xps or *.oxps format.

You need to consider converting xps|oxps to .jpg

change the extesion of file to xps

PrintFileName = @"D:\f.xps"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM