웹에 공개되어 있는 Sept '05 WinFX CTP 예제가 아래와 같습니다.
// Save XPS Document page(s) to .bmp
// Current as of Sept '05 WinFX CTP
using System.IO;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using System.Windows.Media.Imaging;
namespace GenerateXpsBitmaps
{
static public class XpsBitmapHelper
{
static public void SaveXpsPageToBitmap(string xpsFileName, int[] pages)
{
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
FixedDocumentSequence docSeq = xpsDoc.GetPackageRoot();
// You can get the total page count from docSeq.PageCount
foreach (int pageNum in pages)
{
DocumentPage docPage = docSeq.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.Bgra32);
renderTarget.Render(docPage.Visual);
BitmapEncoder encoder = new BmpBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
FileStream pageOutStream = new FileStream(xpsDoc + ".Page" + pageNum + ".bmp", FileMode.Create, FileAccess.Write);
encoder.Save(pageOutStream);
pageOutStream.Close();
}
}
}
}
물론,,, ^^; Feb '06 WinFX CTP에서는 위의 코드가 정상적으로 동작하지 않습니다.
아래의 코드와 같이 변경이 되었기 때문입니다.
또한, 정식 릴리즈되는 WinFX 버전에서는 아래의 코드도 동작하지 않을 것입니다. 참고하십시오. ^^
using System.IO;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using System.Windows.Media.Imaging;
using System.Windows.Xps.Serialization;
namespace WindowsApplication1
{
public class XpsBitmapHelper
{
static public void SaveXpsPageToBitmap(string xpsFileName, int[] pages)
{
XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
FixedDocumentSequence fixedDocumentSeq = xpsDoc.GetFixedDocumentSequence();
for ( int i = 0; i < fixedDocumentSeq.DocumentPaginator.PageCount; i ++ )
{
DocumentPage aPage = fixedDocumentSeq.DocumentPaginator.GetPage( i );
BitmapImage bitmap = new BitmapImage();
RenderTargetBitmap renderTarget =
new RenderTargetBitmap((int)aPage.Size.Width,
(int)aPage.Size.Height,
96, // WPF (Avalon) units are 96dpi based
96,
System.Windows.Media.PixelFormats.Default);
renderTarget.Render(aPage.Visual);
BitmapEncoder encoder = new BmpBitmapEncoder(); // Choose type here ie: JpegBitmapEncoder, etc
encoder.Frames.Add(BitmapFrame.Create(renderTarget));
FileStream pageOutStream = new FileStream(xpsDoc + ".Page" + i.ToString() + ".bmp", FileMode.Create, FileAccess.Write);
encoder.Save(pageOutStream);
pageOutStream.Close();
}
}
}
}