UWP C# xaml image printing glitch

VoyTec 651 Reputation points

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.

Universal Windows Platform (UWP)
No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AryaDing-MSFT 2,826 Reputation points


    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"/>  

    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");  

    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.