我试图在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。有谁知道这里可能出现什么问题?