none
How to convert XPS to Bitmap?

    Question

  • I think my question in a resolved issue got lost, so I ask this fresh:

     

    I'm too looking for a way to convert xps to bitmap, and found this code:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=131564&SiteID=1

    Pity is, it does not even compile on my system. I always get these two errors:

     

    error CS1061: 'System.Windows.Xps.Packaging.XpsDocument' does not contain a definition for 'GetPackageRoot' and no extension method 'GetPackageRoot' accepting a first argument of type 'System.Windows.Xps.Packaging.XpsDocument' could be found (are you missing a using directive or an assembly reference?)

    error CS1061: 'System.Windows.Documents.FixedDocumentSequence' does not contain a definition for 'GetPage' and no extension method 'GetPage' accepting a first argument of type 'System.Windows.Documents.FixedDocumentSequence' could be found (are you missing a using directive or an assembly reference?)

     

    Have these method names changed? I can't think (or find) any reference I might be missing

     

    Or can someone just show me an example of how to convert XPS to Bitmap?

     

    Thanks in advance,
    Sam

    Monday, February 18, 2008 9:11 AM

Answers

  • something along these lines would work

    Code Snippet

    XpsDocument xpsDoc = new XpsDocument(@"c:\users\lee\doc1.xps", System.IO.FileAccess.Read);

    FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

    DocumentReferenceCollection drc = docSeq.References;

    foreach(DocumentReference dr in drc){

    FixedDocument dp = dr.GetDocument(false);

    PageContent pc = dp.Pages[0];

    FixedPage fixedPage = pc.GetPageRoot(false);

    double width = fixedPage.Width;

    double height = fixedPage.Height;

    Size sz = new Size(width, height);

    fixedPage.Measure(sz);

    fixedPage.Arrange(new Rect(new Point(), sz));

    fixedPage.UpdateLayout();

    BitmapImage bitmap = new BitmapImage();

    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)width,(int)height,96,96,System.Windows.Media.PixelFormats.Default);

    renderTarget.Render(fixedPage);

    BitmapEncoder encoder = new BmpBitmapEncoder();

    encoder.Frames.Add(BitmapFrame.Create(renderTarget));

    FileStream pageOutStream = new FileStream(@"c:\users\lee\doc1" + ".Page1.bmp", FileMode.Create, FileAccess.Write);

    encoder.Save(pageOutStream);

    pageOutStream.Close();

    }

     

     

     

    Monday, February 18, 2008 1:25 PM
    Moderator

All replies

  • something along these lines would work

    Code Snippet

    XpsDocument xpsDoc = new XpsDocument(@"c:\users\lee\doc1.xps", System.IO.FileAccess.Read);

    FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

    DocumentReferenceCollection drc = docSeq.References;

    foreach(DocumentReference dr in drc){

    FixedDocument dp = dr.GetDocument(false);

    PageContent pc = dp.Pages[0];

    FixedPage fixedPage = pc.GetPageRoot(false);

    double width = fixedPage.Width;

    double height = fixedPage.Height;

    Size sz = new Size(width, height);

    fixedPage.Measure(sz);

    fixedPage.Arrange(new Rect(new Point(), sz));

    fixedPage.UpdateLayout();

    BitmapImage bitmap = new BitmapImage();

    RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)width,(int)height,96,96,System.Windows.Media.PixelFormats.Default);

    renderTarget.Render(fixedPage);

    BitmapEncoder encoder = new BmpBitmapEncoder();

    encoder.Frames.Add(BitmapFrame.Create(renderTarget));

    FileStream pageOutStream = new FileStream(@"c:\users\lee\doc1" + ".Page1.bmp", FileMode.Create, FileAccess.Write);

    encoder.Save(pageOutStream);

    pageOutStream.Close();

    }

     

     

     

    Monday, February 18, 2008 1:25 PM
    Moderator
  • Yup, this nice little something did work, thanks a lot!

     

    Sam

    Monday, February 18, 2008 5:07 PM