question

Voytec avatar image
0 Votes"
Voytec asked AryaDing-MSFT commented

UWP C# xaml image printing glitch

Greeting MSDN,

I am facing a problem of not showing image to print for the first time:
I am using Print Sample from GitHub - I don't know how can I fix a glitch of not printing an image for the first time - I have to click print button few times to show it up... other images do show.

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

AryaDing-MSFT avatar image
0 Votes"
AryaDing-MSFT answered AryaDing-MSFT commented

Hi,

Welcome to Microsoft Q&A!

Maybe your image isn’t initialized or load completely when the preview window displays. Please refer to the following steps to resolve this issue.

1.Provide a Canvas control and make it completely transparent(Opacity="0"), so that it doesn’t interfere with your UI.

 <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
         <Canvas Name="MyCanvas" Opacity="0"/>
         <Button x:Name="btnPrint" Click="btnPrint_Click" Content="Print" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center"/>
 </Grid>

2.Add a PreparePrintContent method and call it before the PrintManager.ShowPrintUIAsync in the btnPrint_Click event, as follows:

 private async void btnPrint_Click(object sender, RoutedEventArgs e)
 {
     if (printDoc != null)
     {
         printDoc.GetPreviewPage -= OnGetPreviewPage;
         printDoc.Paginate -= PrintDic_Paginate;
         printDoc.AddPages -= PrintDic_AddPages;
     }
     this.printDoc = new PrintDocument();
     printDoc.GetPreviewPage += OnGetPreviewPage;
     printDoc.Paginate += PrintDic_Paginate;
     printDoc.AddPages += PrintDic_AddPages;
    
     PreparePrintContent(new PageToPrint());
    
     bool showPrint = await PrintManager.ShowPrintUIAsync();
 }
    
 private void PreparePrintContent(Page pageToPrint)
 {
     var canvas=(Canvas)this.FindName("MyCanvas");
     canvas.Children.Clear();
     canvas.Children.Add(pageToPrint);
 }


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.





· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

What is "PrintDoc" and "PrintDic"?
@AryaDing-MSFT

0 Votes 0 ·

@Voytec-6484 PrintDoc is the PrintDocument Type object. As for "PrintDic_Paginate, PrintDic_AddPages", it is event handler, its event name isn't important, you could change it at will. You could compare this document.

0 Votes 0 ·