[英]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.