
C#でXPSをJPGに変換する方法
XPS ドキュメント形式を使用すると、デジタル ドキュメントを簡単に共有、作成、印刷、および保存できますが、サポートするソフトウェアをインストールする必要がある場合があります。あなたが .NET プログラマーで、XPS ファイルをプログラムで JPG/JPEG 形式にエクスポートする場合、この記事では C# で XPS を JPG に変換する方法 について説明します。
この記事では、次の点について説明します。
XPS を JPG に変換する C# .NET API
この記事では、Conholdate.Total for .NET を使用して XPS を JPG に変換します。このライブラリを使用すると、既存の XPS ドキュメントと新しい XPS ドキュメントを作成、編集、および保存できます。したがって、DLL を ダウンロード するか、NuGet を使用してこの XPS 変換 API をインストールできます。
Install-Package Conholdate.Total
C# でプログラムによって XPS を JPG/JPEG に変換する方法
XPS 変換 API を使用すると、XPS ファイルを JPG に変換できます。
XPS ファイルを JPG 画像に変換する手順は次のとおりです。
- XPS ドキュメントの Stream クラスのオブジェクトを初期化します。
- 前の手順で作成した XPS ストリームと XpsLoadOptions クラスのオブジェクトをパラメーターとして使用して、XpsDocument クラスのインスタンスを作成します。
- JpegSaveOptions クラスのオブジェクトを必要なパラメータで初期化します。
- ImageDevice クラスのインスタンスを作成します。
- XpsDocument.Save(Device device, SaveOptions options) を呼び出して、JPG を ImageDevice オブジェクトに保存します。
- ImageDevice を使用して JPG をディスクに保存します (以下のコード サンプルを参照)。
次のコード サンプルは、C# を使用して XPS を JPG に変換する方法を示しています。
string inputFileName = "input.xps"; | |
//Outut file | |
string outputFileName = "XPStoImage_out.jpg"; | |
// Initialize XPS input stream | |
using (Stream xpsStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read)) | |
{ | |
// Load XPS document form the stream | |
XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions()); | |
// Initialize options object with necessary parameters. | |
JpegSaveOptions options = new JpegSaveOptions() | |
{ | |
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality, | |
Resolution = 300, | |
PageNumbers = new int[] { 1, 2, 6 } | |
}; | |
// Create rendering device for image | |
ImageDevice device = new ImageDevice(); | |
document.Save(device, options); | |
// Iterate through document partitions (fixed documents, in XPS terms) | |
for (int i = 0; i < device.Result.Length; i++) | |
// Iterate through partition pages | |
for (int j = 0; j < device.Result[i].Length; j++) | |
{ | |
// Initialize image output stream | |
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + | |
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) + | |
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write)) | |
// Write image | |
imageStream.Write(device.Result[i][j], 0, device.Result[i][j].Length); | |
} | |
} |
無料ライセンスを取得する
無料の一時ライセンス を取得して、評価制限なしで API をテストできます。
結論
この記事を締めくくるにあたり、XPS 変換 API を使用して C# で XPS を JPG に変換する方法を学習していただければ幸いです。 XPS 変換ライブラリのその他の機能については、ドキュメント を参照してください。
質問する
フォーラムで質問やクエリをお知らせください。