What are XPS files and how are they used

I have a simple C # .net web application. In this I work with XPS files. I used the following code

private void button1_Click(object sender, EventArgs e) { try { string xpsFile = "D:\\Completed-Form.xps"; xpsToBmp(xpsFile); MessageBox.Show("Done"); } catch (Exception ex) { MessageBox.Show (ex.Message); } } static public void xpsToBmp(string xpsFile) { XpsDocument xps = new XpsDocument(xpsFile, System.IO.FileAccess.Read); FixedDocumentSequence sequence = xps.GetFixedDocumentSequence(); for (int pageCount = 0; pageCount < sequence.DocumentPaginator.PageCount; ++pageCount) { DocumentPage page = sequence.DocumentPaginator.GetPage(pageCount); RenderTargetBitmap toBitmap = new RenderTargetBitmap((int)page.Size.Width,(int)page.Size.Height,96,96,System.Windows.Media.PixelFormats.Default); toBitmap.Render(page.Visual); BitmapEncoder bmpEncoder = new BmpBitmapEncoder(); bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap)); FileStream fStream = new FileStream("D:\\xpstobmp" + pageCount + ".bmp", FileMode.Create, FileAccess.Write); bmpEncoder.Save(fStream); fStream.Close(); } } 

When I debug the code, an error appears showing XamlParserException

'An exception was made when calling a constructor of the type' System.Windows.Documents.DocumentReference 'that matches the specified binding constraints.' Line number "2" and line position "20".

in the following line of code:

 FixedDocumentSequence sequence = xps.GetFixedDocumentSequence(); 

I downloaded a sample XPS file from http://msdn.microsoft.com/en-us/library/windows/hardware/gg463422.aspx (I got a 160 megabyte zip file from there. When I unzip it, the number of folders and files with the extension .xps. I do not know how to use these files) and is used in the above code. I am very new to this file concept. I do not know how to solve this error and how .xps files are used. I also have little information about bitmap files.

+6
source share
2 answers

Even while working with Windows, I ran into the same problem.

Decision:

The calling stream must be in STA mode. In most projects created by Visual Studio, the default value is MTA.

What you can do is run the code inside the STA thread.

I tried: Visual Studio 2010, Windows XP Srv Pack 3 64 bit, and .Net Framework 4.0

Luck...

Take this as an answer if it solves the problem

+4
source

Your code works, I just tested my environment (VS 2010, Windows 7 64bit).

As an input file, I used a google page printed with the built-in Microsoft XPS Document Writer.

So the problem is with the XPS document you are testing.

+1
source

All Articles