简体   繁体   中英

Image not visible when printing

I need to implement printing in my Win 8.1 app and a key requirement is to be able to generate the page after the printer has been selected and the print button has been clicked. This is driven off security requirements around the images and is not negotiable. Eventually this is the point where I'll have to download the images needed in printing.

Currently for my testing I'm using an image that is local to the project:

string testImageLocalSource = "ms-appx:///Assets/testImage.png";

In my test project I'm working in, I'm generating the print page during the PrintDocument.AddPages event handler as follows (layout/margin code removed for succinctness):

    private void PrintDocument_AddPages(object sender, AddPagesEventArgs e)
    {
        var printPageDescription = e.PrintTaskOptions.GetPageDescription(0);
        FrameworkElement printPage;

        printPage = new MainPrintPage();

        // get the printable content
        Grid printableArea = (Grid)printPage.FindName("printableArea");

        Run myRun1 = new Run();
        myRun1.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
        myRun1.FontStyle = Windows.UI.Text.FontStyle.Oblique;
        myRun1.Foreground = new SolidColorBrush(Windows.UI.Colors.Purple);
        myRun1.FontSize = 42;

        Image i = new Image();
        i.Source = new BitmapImage(new Uri(testImageLocalSource, UriKind.Absolute));
        i.Height = 200;

        InlineUIContainer container = new InlineUIContainer();
        container.Child = i;

        // Create a paragraph and add the content.
        Paragraph myParagraph = new Paragraph();
        myParagraph.Inlines.Add(container);
        myParagraph.Inlines.Add(myRun1);

        // add paragraph to RichTextBlock blocks
        var mainRTB = printPage.FindName("mainrtb");
        ((RichTextBlock)mainRTB).Blocks.Add(myParagraph);

        // add page to hidden canvas
        printingRoot.Children.Add(printPage);
        printingRoot.InvalidateMeasure();
        printingRoot.UpdateLayout();

        printDocument.AddPage(printPage);

        PrintDocument printDoc = (PrintDocument)sender;
        printDoc.AddPagesComplete();

    }

The text appears fine, and there seems to be extra space where the image should be, but the image doesn't show up.

The image shows up in the print if I use this code in an earlier event handler, such as PrintDocument.Paginate .

Has anybody tried to do something similar and found a solution or else does anybody have an explanation as to what is going on here and an idea on how to remedy it?

UPDATE
I'm attempting to move a bulk of this code to the PrintTask.Submitting event and this is showing promise. I'll update this with an example if it works.

您是否忘记设置图像的宽度?

i.Width = 200;

不完全确定是什么导致了Win 8.1中的问题,但似乎已在Windows 10中修复。

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