无法将 PdfPage 作为 BitmapImage 保存到磁盘

Can't save PdfPage as a BitmapImage to disk

我正在尝试将 PdfDocument 的页面作为一组 BitmapImage 保存到磁盘中,为此我正在使用 WinRTXamlToolkit 提供的扩展。

PdfPage page = pdfd.GetPage(0); // pdfd is a loaded PdfDocument
InMemoryRandomAccessStream str=new InMemoryRandomAccessStream();
await page.RenderToStreamAsync(str);
BitmapImage bmp = new BitmapImage();
await bmp.SetSourceAsync(str);

WriteableBitmap wb = await WriteableBitmapFromBitmapImageExtension.FromBitmapImage(bmp);

StorageFolder sfo;
sfo = await KnownFolders.DocumentsLibrary.CreateFolderAsync("PDFasBMP", CreationCollisionOption.OpenIfExists);

await WriteableBitmapSaveExtensions.SaveToFile(wb,sfo,"Yahooo.bmp");

输出 "Yahooo.bmp" 是一个不可读的 58 字节文件。我错过了什么?该问题与 WinRTXamlToolkit 无关,因为当 BitmapImage 设置为网络中的图像时,它会正确保存到磁盘,因此问题出在 PdfPage 的工作方式上。

跳过 BitmapImage 并将 PDF 直接加载到 WriteableBitmap 中:

WriteableBitmap wb = new WriteableBitmap(1,1);
await wb.SetSourceAsync(str);

不可能从 BitmapImage 中提取像素,因此 WriteableBitmapFromBitmapImageExtension.FromBitmapImage 必须从其他地方获取数据:当 BitmapImage 从网络加载时它起作用,因为 FromBitmapImage 可以获取 BitmapImage 的 UriSource 并加载将该 URI 放入 WriteableBitmap。