3

Windows 8 で PrintDocument を使用して印刷しようとしています。ただし、StackPanel に大きな画像が存在する場合、あらゆる種類の問題が発生します。空のページが表示されることもあれば、プログラムが printDoc.AddPage でスタックすることもあれば、PrintTask がエラーで完了することもあります。画像を小さい画像に変更すると、すべてうまくいきます。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