[英]Image not visible when printing
我需要在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中修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.