(C# XPS) Printer prints low quality image

39 views Asked by At

I wrote a small program that prints a document with a grid of 1x1in images. The problem is that its final output has images with low resolution. I tried replicating it using Inkscape and its quality is high. The source image is around 1500x1500px.

Here's the code for printing:

private void OnPrintClick(object sender, RoutedEventArgs e)
        {
            if (fixedDoc != null)
            {
                if (CurrentPrintQueue != null)
                {
                    PrintTicket printTicket = CurrentPrintQueue.DefaultPrintTicket;
                    printTicket.OutputQuality = OutputQuality.High;
                    printTicket.PageMediaSize = new PageMediaSize(paperWidth * unitInch, paperHeight * unitInch);
                    XpsDocumentWriter writer = PrintQueue.CreateXpsDocumentWriter(CurrentPrintQueue);
                    writer.Write(fixedDoc, printTicket);
                }
                else
                {
                    MessageBox.Show("No printer selected.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
                }
            }
            else
            {
                MessageBox.Show("No Document to print.", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

I already tried changing the PageResolution to 192 or 300 but nothing's happening.

Here's the code for setting up the FixedDocument:

 private void SetupDocument(ImageSource? image, double width, double height)
        {
            if (image == null) return;
            FixedPage page = SetupPage(SetupUI(image, width, height));
            PageContent pc = new PageContent();
            IAddChild iac = (IAddChild)pc;
            iac.AddChild(page);
            fixedDoc = new FixedDocument();
            fixedDoc.Pages.Add(pc);
            PageViewer.Document = fixedDoc;
        }

Here's the code for setting up the FixedPage:

private FixedPage SetupPage(UIElement ui)
        {
            FixedPage page = new FixedPage();
            page.Width = paperWidth * unitInch;
            page.Height = paperHeight * unitInch;
            page.Children.Add(ui);
            FixedPage.SetLeft(ui, 0);
            FixedPage.SetTop(ui, 0);

            Size sz = new Size(paperWidth * unitInch, paperHeight * unitInch);
            page.Measure(sz);
            page.Arrange(new Rect(new Point(), sz));
            page.UpdateLayout();

            return page;
        }

Constants:

        private const double paperWidth = 8.27;
        private const double paperHeight = 11.69;
        private const double unitInch = 96;

The UIElement is a Canvas with a bunch of Image controls.

I'm new in XPS printing so I'm absolutely clueless.

0

There are 0 answers