6

シンプルな 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 ファイルの使用方法がわかりません。また、ビットマップファイルについての知識もほとんどありません。

4

2 に答える 2

1

あなたのコードは機能しています。私の環境(VS 2010、Windows 7 64ビット)でテストしました。

入力ファイルとして、組み込みの Microsoft XPS Document Writer で印刷された Google ページを使用しました。

したがって、問題はテストしている XPS ドキュメントにあります。

于 2013-04-03T16:53:30.217 に答える