What is XPS files and how it is being used

独自空忆成欢 提交于 2019-12-22 05:30:12

问题


I have a simple C# .net web application. In that I'm working with XPS files. I have 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 showing as XamlParserException occurred

'The invocation of the constructor on type 'System.Windows.Documents.DocumentReference' that matches the specified binding constraints threw an exception.' Line number '2' and line position '20'.

in the following line of code:

FixedDocumentSequence sequence = xps.GetFixedDocumentSequence();

I have downloaded a sample XPS file from http://msdn.microsoft.com/en-us/library/windows/hardware/gg463422.aspx (I got 160MB zip file from there. When I unzip it there were a number of folders and files having .xps extension. I don't know how to use these files) and used in the above code. I'm very new to this file concept. I don't know how to resolve this error and how the .xps files are used. Also I have little knowledge about bitmap files.


回答1:


Even i faced the same problem while running as a Windows Application.

The solution to that is :

The calling thread must be in STA mode. Most project created by Visual Studio is set to MTA by default.

What you can do is to run your code inside STA thread.

I tried in: Visual Studio 2010, Windows XP Srv Pack 3 64 Bit, And .Net Framework 4.0

Good Luck...

Accept this as Answer if it solves your issue




回答2:


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

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

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



来源:https://stackoverflow.com/questions/15758497/what-is-xps-files-and-how-it-is-being-used

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!