XPS文件格式经常在文件处理应用程序中使用。您可以在基于.NET Framework的应用程序中使用C#或VB.NET以编程方式将XPS或OXPS转换为JPG或PNG图像。
让我们逐步介绍以下方案,以了解有关XPS转换的更多信息:
- 使用C#或VB.NET将XPS,OXPS转换为JPG图像
- 使用C#或VB.NET将XPS,OXPS转换为PNG图像
目前,.NET版Aspose.page升级到v20.10版,感兴趣的朋友可点击下载
使用C#或VB.NET将XPS,OXPS转换为JPG图像
JPG文件之所以出名是因为它们在所有系统环境中均受支持。您可以使用C#或VB.NET以编程方式将XPS或OXPS文件转换为JPG图像。对于XPS或OXPS到JPG图像的转换,应遵循以下步骤:
- 加载输入XPS或OXPS文件
- 初始化JpegSaveOptions对象
- 指定要渲染的SmoothingMode,Resolution和PageNumbers
- 保存输出的JPG图像
以下代码显示了如何使用C#语言将XPS或OXPS转换为JPG图像:
// Input file
string inputFileName = dataDir + "input.xps";
//Output file
string outputFileName = dataDir + "XPStoImage_out.jpeg";
// 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());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
JpegSaveOptions options = new JpegSaveOptions()
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
Resolution = 300
};
// Create rendering device for JPG format
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); } }
使用C#或VB.NET将XPS,OXPS转换为PNG图像
可能需要使用C#或VB.NET和Aspose.Page for .NET API将XPS或OXPS文件转换为PNG。您需要按照以下步骤将XPS转换为PNG:
- 加载输入XPS或OXPS文件
- 初始化PngSaveOptions对象
- 设置图像分辨率或页面编号为渲染
- 保存输出的PNG图像
下面的代码段显示了如何使用C#以编程方式将XPS或OXPS文件转换为PNG图像:
// Input file
string inputFileName = dataDir + "input.xps";
//Outut file
string outputFileName = dataDir + "XPStoImage_out.png";
// 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());
// or load XPS document directly from file. No xpsStream is needed then.
// XpsDocument document = new XpsDocument(inputFileName, new XpsLoadOptions());
// Initialize options object with necessary parameters.
PngSaveOptions options = new PngSaveOptions()
{
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality,
Resolution = 300
};
// Create rendering device for PNG format
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); } }
如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。
来源:oschina
链接:https://my.oschina.net/u/4087915/blog/4714587