简体   繁体   English

打印时图像不可见

[英]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. 我需要在Win 8.1应用程序中实现打印,一个关键的要求是能够在选择打印机并单击“打印”按钮生成页面。 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): 在我正在从事的测试项目中,我将在PrintDocument.AddPages事件处理程序中生成打印页面,如下所示(为简洁起见,删除了布局/边距代码):

    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 . 如果我在早期的事件处理程序(例如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. 我试图将这段代码的大部分移至PrintTask.Submitting事件,这显示了承诺。 I'll update this with an example if it works. 如果可行,我将用一个示例来更新它。

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

i.Width = 200;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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