UWP C# xaml image printing glitch

VoyTec 671 Reputation points
2021-06-27T00:17:14.607+00:00

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)
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AryaDing-MSFT 2,916 Reputation points
    2021-06-28T07:47:56.713+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.