打印时图像不可见
Image not visible when printing
我需要在我的 Win 8.1 应用程序中实现打印,关键要求是能够在选择打印机并单击打印按钮后生成页面.这是出于对图像的安全要求的考虑,并且是不可协商的。最终这是我必须下载打印所需图像的地方。
目前我的测试使用的是项目本地的图像:
string testImageLocalSource = "ms-appx:///Assets/testImage.png";
在我工作的测试项目中,我在 PrintDocument.AddPages
事件处理程序期间生成打印页面,如下所示(layout/margin 为简洁起见删除了代码):
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();
}
文本显示正常,图像应该在的地方似乎多了 space,但图像没有显示。
如果我在较早的事件处理程序中使用此代码,例如 PrintDocument.Paginate
。
,图像会显示在打印件中
有没有人尝试做类似的事情并找到了解决方案,或者是否有人对这里发生的事情有解释以及如何补救的想法?
更新
我正在尝试将此代码的大部分移动到 PrintTask.Submitting
事件中,这显示出希望。如果它有效,我会用一个例子来更新它。
你是不是忘记设置图片的宽度了?
i.Width = 200;
不确定是什么导致了 Win 8.1 中的这种情况,但似乎已在 Windows 10 中修复。
我需要在我的 Win 8.1 应用程序中实现打印,关键要求是能够在选择打印机并单击打印按钮后生成页面.这是出于对图像的安全要求的考虑,并且是不可协商的。最终这是我必须下载打印所需图像的地方。
目前我的测试使用的是项目本地的图像:
string testImageLocalSource = "ms-appx:///Assets/testImage.png";
在我工作的测试项目中,我在 PrintDocument.AddPages
事件处理程序期间生成打印页面,如下所示(layout/margin 为简洁起见删除了代码):
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();
}
文本显示正常,图像应该在的地方似乎多了 space,但图像没有显示。
如果我在较早的事件处理程序中使用此代码,例如 PrintDocument.Paginate
。
有没有人尝试做类似的事情并找到了解决方案,或者是否有人对这里发生的事情有解释以及如何补救的想法?
更新
我正在尝试将此代码的大部分移动到 PrintTask.Submitting
事件中,这显示出希望。如果它有效,我会用一个例子来更新它。
你是不是忘记设置图片的宽度了?
i.Width = 200;
不确定是什么导致了 Win 8.1 中的这种情况,但似乎已在 Windows 10 中修复。