3

我正在尝试使用 Windows 8 中的 PrintDocument 进行打印。但是,当 StackPanel 中存在大图像时,我会遇到各种问题。有时我得到空白页,在其他情况下,程序卡在 printDoc.AddPage 中,在其他情况下,PrintTask 完成时出现错误。如果我将 imgae 更改为较小的图像,一切都会很好。如果我尝试使用 DecodePixelWidth 调整图像大小,有时效果很好,而在其他情况下则不行(PringtTask 完成时出现错误)。这是我正在使用的代码:

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
        for (int i = 0; i < 6; i++)
        {
            //1. Prints empty page
            BitmapImage bmi = new BitmapImage(new Uri("http://www.colby.edu/academics_cs/ocs/students/images/Spatz_Charles_13_NewZealand.jpg"));
            //2. Gets stuck on printDoc.AddPage(viewer)
            // BitmapImage bmi = new BitmapImage(new Uri("http://littlefieldreport.files.wordpress.com/2012/10/new-zealand-high-view.jpg"));
            //3. Works well
            //BitmapImage bmi = new BitmapImage(new Uri("http://www.touristmaker.com/images/new-zealand/new-zealand-forest.jpg"));
            //bmi.DecodePixelWidth = 600;
            Image image = new Image()
            {
                Source = bmi
            };
            image.Width = 150;
            panel.Children.Add(image);
        }
        panel.UpdateLayout();
    }

    private PrintDocument printDocument = null;
    private IPrintDocumentSource printDocumentSource = null;
    private void PrintTaskRequested(PrintManager sender, PrintTaskRequestedEventArgs e)
    {
        PrintTask printTask = null;
        printTask = e.Request.CreatePrintTask("Print Task", sourceRequested =>
        {
            IList<string> displayedOptions = printTask.Options.DisplayedOptions;

            // Choose the printer options to be shown.
            // The order in which the options are appended determines the order in which they appear in the UI
            displayedOptions.Clear();
            displayedOptions.Add(StandardPrintTaskOptions.Copies);
            displayedOptions.Add(StandardPrintTaskOptions.Orientation);
            displayedOptions.Add(StandardPrintTaskOptions.MediaSize);
            displayedOptions.Add(StandardPrintTaskOptions.Collation);
            displayedOptions.Add(StandardPrintTaskOptions.Duplex);

            printTask.Options.MediaSize = PrintMediaSize.NorthAmericaLegal;


            printTask.Completed += async (s, args) =>
            {
                if (args.Completion == PrintTaskCompletion.Failed)
                {
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                    {
                        //rootPage.NotifyUser("Failed to print.", NotifyType.ErrorMessage);
                    });
                }
            };

            sourceRequested.SetSource(printDocumentSource);
        });
    }

    private void RegisterForPrinting()
    {
        printDocument = new PrintDocument();
        printDocumentSource = printDocument.DocumentSource;
        printDocument.Paginate += CreatePrintPreviewPages;
        printDocument.GetPreviewPage += GetPrintPreviewPage;
        printDocument.AddPages += AddPrintPages;
        PrintManager printMan = PrintManager.GetForCurrentView();
        printMan.PrintTaskRequested += PrintTaskRequested;
    }

    private void UnregisterForPrinting()
    {
        if (printDocument == null)
            return;

        printDocument.Paginate -= CreatePrintPreviewPages;
        printDocument.GetPreviewPage -= GetPrintPreviewPage;
        printDocument.AddPages -= AddPrintPages;

        PrintManager printMan = PrintManager.GetForCurrentView();
        printMan.PrintTaskRequested -= PrintTaskRequested;
    }

     private void CreatePrintPreviewPages(object sender, PaginateEventArgs e)
    {
        PrintDocument printDoc = (PrintDocument)sender;
        printDoc.SetPreviewPageCount(1, PreviewPageCountType.Intermediate);
    }

    private void GetPrintPreviewPage(object sender, GetPreviewPageEventArgs e)
    {
        PrintDocument printDoc = (PrintDocument)sender;

        printDoc.SetPreviewPage(e.PageNumber, viewer);
    }

    private void AddPrintPages(object sender, AddPagesEventArgs e)
    {
        PrintDocument printDoc = (PrintDocument) sender;
        printDoc.AddPage(viewer);

        printDoc.AddPagesComplete();
    }

    #region Navigation
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        RegisterForPrinting();
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        UnregisterForPrinting();
    }
    #endregion

}

MainPage 在 ScrollViewer 中包含一个 StackPanel。有谁知道这里可能是什么问题?

4

0 回答 0