什么是XPS文件,以及它是如何被使用它是、文件、XPS

2023-09-04 02:39:29 作者:青歌

我有一个简单的C#.NET Web应用程序。在此我正在使用XPS文件。我使用了下列code

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

当我调试code,发生显示为 XamlParserException 错误

When I debug the code, an error showing as XamlParserException occurred

System.Windows.Documents.DocumentReference在类型构造函数的调用'符合指定绑定约束引发了异常。行号'2'和线位20。

'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'.

在code以下行:

FixedDocumentSequence sequence = xps.GetFixedDocumentSequence();

//:

我从 HTTP下载的样本XPS文件msdn.microsoft.com/en-us/library/windows/hardware/gg463422.aspx (我有160MB的zip文件从那里。当我将它解压缩有一些具有.xps扩展名的文件夹和文件。我不知道如何使用这些文件),并在上面code使用。我很新的这个文件的概念。我不知道如何解决这个错误,以及如何.xps文件被使用。我也有关于位图文件知之甚少。

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.

推荐答案

即使我在运行一个Windows应用程序面临同样的问题。

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

的解决方案是:

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

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

你可以做的就是运行code STA线程里面。

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

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

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

好运...

接受这是回答能否解决你的问题

 
精彩推荐
图片推荐