日期:2014-05-17 浏览次数:20933 次
Bitmap image = null;
FixedDocumentSequence docs = xps.GetFixedDocumentSequence();
using (DocumentPage docPage = docs.DocumentPaginator.GetPage(page))
{
float dpi = ConfigManager.DocDpi;
int width = (int)(docPage.Size.Width * dpi / 96 + 0.5);
int height = (int)(docPage.Size.Height * dpi / 96 + 0.5);
RenderTargetBitmap renderTarget = new RenderTargetBitmap(width, height, dpi, dpi, System.Windows.Media.PixelFormats.Default);
renderTarget.Render(docPage.Visual);
// calling GetPage without calling UpdateLayout causes a memory leak
((FixedPage)docPage.Visual).UpdateLayout();
BitmapEncoder encoder = new BmpBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
MemoryStream s = new MemoryStream();
encoder.Save(s);
image = new Bitmap(s);
image.SetResolution(dpi, dpi);
}