无法将 PdfPage 作为位图图像保存到磁盘



我正试图将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设置为web中的映像时,它会正确地保存到磁盘上,所以问题在于PdfPage的工作方式。

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

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

无法从BitmapImage中提取像素,因此WriteableBitmapFromBitmapImageExtension.FromBitmapImage必须从其他地方获取数据:当从web加载BitmapImage时,它就可以工作,因为FromBitmapImage可以获取BitmapImage的UriSource并将该URI加载到WriteableBitmap中。

最新更新