Search code examples
c#printdocument

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.

enter image description here


Solution

  • 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"