【问题标题】:Print preview does not render images打印预览不渲染图像
【发布时间】:2015-12-06 10:56:18
【问题描述】:

我正在尝试从我的通用 Windows 应用商店应用打印页面。

我目前设置了一个元组列表,(包含要放在打印页面上的数据:stringImageSource),当用户想要打印时,一个新的@987654323 @(Page 的自定义子类,使用 XAML 设置样式)被实例化,我使用以下代码在 PrintPage 上设置元素:

public PrintPage(Tuple<string, ImageSource> tuple) {
    Title.Text = tuple.Item1;
    Image.Source = tuple.Item2;
}

我的 XAML 在哪里:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="10*"/>
    </Grid.RowDefinitions>
    <TextBlock Name="Title"/>
    <Image Grid.Row="1" Name="Image" Stretch="Uniform"/>
</Grid>

ImageSource 使用以下代码呈现(并显示在页面上让用户选择要打印的内容):

RenderTargetBitmap Bitmap = new RenderTargetBitmap();
await Bitmap.RenderAsync(DrawingArea); // DrawingArea is a `Grid` containing my elements

现在当我打印我的页面时,在预览和打印页面上,我看不到我的图像。我做错了什么?

【问题讨论】:

  • 只是猜测,但您不需要等待 RenderAsync 吗?
  • 是的,这是一个错字,编辑了我的问题

标签: wpf xaml windows-store-apps uwp printdocument


【解决方案1】:

它不会显示,因为它是 RenderTargetBitmap,您需要将其转换为 BitmapImage,如下所示:

private async void PrintButton_Click(object sender, RoutedEventArgs e) { var renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(MyControl); var bitmapImage = await ConvertToBitmapImage(renderTargetBitmap); var grid = new Grid { HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }; var image = new Image { Source = bitmapImage, Stretch = Windows.UI.Xaml.Media.Stretch.Uniform, HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Stretch }; grid.Children.Add(image); _printHelper.AddFrameworkElementToPrint(grid); await _printHelper.ShowPrintUIAsync("", new PrintHelperOptions { Orientation = Windows.Graphics.Printing.PrintOrientation.Landscape }); } private async Task<BitmapImage> ConvertToBitmapImage(RenderTargetBitmap renderTargetBitmap) { var pixels = await renderTargetBitmap.GetPixelsAsync(); var stream = pixels.AsStream(); var outStream = new InMemoryRandomAccessStream(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, outStream); var displayInformation = DisplayInformation.GetForCurrentView(); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, pixels.ToArray()); await encoder.FlushAsync(); outStream.Seek(0); var bitmap = new BitmapImage(); await bitmap.SetSourceAsync(outStream); return bitmap; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    • 2014-01-04
    • 2015-10-21
    相关资源
    最近更新 更多