我的以下代码取自http://www.codeplex.com/XPS2Image/,而这些代码又取自 Microsoft Developer's Network 论坛上的讨论。
int[] pages = new int[] { 0, 1, 2, 3, 4 };
XpsDocument xpsDoc = new XpsDocument(@"c:\tmp\sample.xps", System.IO.FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
// You can get the total page count from docSeq.PageCount
foreach (int pageNum in pages)
{
DocumentPaginator paginator = docSeq.DocumentPaginator;
DocumentPage docPage = paginator.GetPage(pageNum);
BitmapImage bitmap = new BitmapImage();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)docPage.Size.Width,
(int)docPage.Size.Height,
96, // WPF (Avalon) units are 96dpi based
96,
System.Windows.Media.PixelFormats.Pbgra32);
renderTarget.Render(docPage.Visual);
PngBitmapEncoder encoder = new PngBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
FileStream pageOutStream = new FileStream("c:\\tmp\\xpsdocPage" + pageNum + ".jpg", FileMode.Create, FileAccess.Write);
encoder.Save(pageOutStream);
pageOutStream.Close();
}
这工作正常,除了未渲染ResourceDictionaries
中使用的遥控器。FixedPage
有任何想法吗?