什么是XPS文件,以及它是如何被使用(What is XPS files and how it is

2019-08-18 23:38发布

我有一个简单的C#.NET Web应用程序。 在我与XPS文件的工作。 我用下面的代码

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();
            }
        }

当我调试的代码,表示作为一个错误XamlParserException发生

System.Windows.Documents.DocumentReference“上类型构造函数的调用‘’匹配指定绑定约束引发了异常。” 行号“2”和线位置“20”。

在下面的代码行:

FixedDocumentSequence sequence = xps.GetFixedDocumentSequence();

我已经下载了从样品XPS文件http://msdn.microsoft.com/en-us/library/windows/hardware/gg463422.aspx (我从那里得到160MB的zip文件。当我把它解压有一些文件夹并且具有文件.XPS扩展。我不知道如何使用这些文件),并在上面的代码中使用。 我非常新的这个文件的概念。 我不知道如何解决这个错误和.XPS文件是如何使用的。 我也有关于位图文件知之甚少。

Answer 1:

连我而作为Windows应用程序运行所面临的同样的问题。

该解决方案是:

调用线程必须为STA模式。 Visual Studio创建大多数项目被默认设置为MTA。

你可以做的是运行STA线程内的代码。

我试着在:Visual Studio 2010中,Windows XP的SRV Pack 3的64位,和.NET Framework 4.0

祝好运...

接受这个作为回答,如果它解决您的问题



Answer 2:

您的代码工作,我只是在我的环境测试(VS 2010,64位Windows 7)。

作为输入文件我用印有内置的Microsoft XPS文档写入一个谷歌的网页。

所以,问题是与XPS文档您正在测试。



文章来源: What is XPS files and how it is being used