C# 打印时图像不可见

C# 打印时图像不可见,c#,printing,windows-8.1,C#,Printing,Windows 8.1,我需要在我的Win 8.1应用程序中实现打印,关键要求是在选择打印机并单击打印按钮后能够生成页面。这是出于图像周围的安全要求,不可协商。最终,我将不得不下载打印所需的图像 目前,在我的测试中,我使用的是项目的本地图像: string testImageLocalSource = "ms-appx:///Assets/testImage.png"; 在我的测试项目中,我在PrintDocument.AddPages事件处理程序中生成打印页面,如下所示:为了简洁起见,删除了布局/边距代码:

我需要在我的Win 8.1应用程序中实现打印,关键要求是在选择打印机并单击打印按钮后能够生成页面。这是出于图像周围的安全要求,不可协商。最终,我将不得不下载打印所需的图像

目前,在我的测试中,我使用的是项目的本地图像:

string testImageLocalSource = "ms-appx:///Assets/testImage.png";
在我的测试项目中,我在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();

    }
文本看起来很好,图像应该在的地方似乎有额外的空间,但图像没有显示出来

如果我在早期的事件处理程序(如PrintDocument.Paginate)中使用此代码,则图像将显示在打印中

有没有人尝试过做类似的事情并找到了解决方案,或者有没有人对这里发生的事情有过解释,以及如何补救的想法

更新
我正试图将这段代码的大部分转移到PrintTask.Submitting事件中,这表明了我的承诺。如果可行的话,我会用一个例子来更新它。

你忘了设置图像的宽度吗

i.Width = 200;

不确定Win 8.1中出现这种情况的原因,但在Windows 10中似乎已经修复。

也尝试过。在这种情况下,宽度和高度不会影响可见性。