在 C# 中以编程方式将 XPS 转换为 BMP

概述

在 C# 中将 XPS 转换为 BMP 对于各种应用程序至关重要。Aspose.Page for .NET 在各个行业中发挥着重要作用。它使开发人员能够以编程方式将 XPS 文件转换为 BMP 格式。同时,Aspose.Page for .NET 通过其易于集成和灵活性简化了该过程。让我们实现如何 在 C# 中以编程方式将 XPS 转换为 BMP

本文将涵盖以下要点:

C# XPS 库安装

要安装 Aspose.Page for .NET,请从 https://releases.aspose.com/page/net/ 下载并运行命令 Install-Package Aspose.Page。由于其先进的自定义选项,该库非常适合在 C# 中将 XPS 转换为 BMP。

在 C# 中将 XPS 转换为 BMP - 代码示例

要使用 Aspose.Page for .NET 在 C# 中将 XPS 转换为 BMP,请按照以下步骤操作:

  1. 实例化 XpsLoadOptions 类的实例。
  2. 通过初始化 XpsDocument 类的实例从 XPS 文件加载 XPS 文档。
  3. 使用必要的参数初始化 BmpSaveOptions 对象。
  4. 调用 SaveAsImage 方法将 XPS 文档保存为图像字节数组。第一维用于内部文档,第二维用于内部文档中的页面。

以下是 C# 代码示例:

// Define the working directory.
string dataDir = "/Desktop";
string outputFileName = dataDir + "XPStoImage_out.bmp";
// Instantiate an instance of the XpsLoadOptions class.
// Load XPS document from the XPS file by initializing an instance of the XpsDocument class.
XpsDocument document = new XpsDocument(dataDir + "input.xps", new XpsLoadOptions());
// Initialize BmpSaveOptions object with necessary parameters.
BmpSaveOptions options = new BmpSaveOptions()
{
SmoothingMode = SmoothingMode.HighQuality,
Resolution = 300,
PageNumbers = new int[] { 1, 2, 6 }
};
// Invoke SaveAsImage method to save XPS document to the images byte arrays. The first dimension is for inner documents and the second one is for pages within inner documents.
byte[][][] imagesBytes = document.SaveAsImage(options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.Length; i++)
{
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].Length; j++)
{
// Initialize image output stream
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar +
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) +
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write))
// Write image
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length);
}
}
view raw XPS-to-BMP.cs hosted with ❤ by GitHub
输出:
xps-library

XPS 文件转换器 - 在线试用

试用由 Aspose.Page for .NET 提供支持的 在线 XPS 转 BMP 转换器。它是免费的,易于使用,并且准确。

xps-file-converter

获取免费许可证

获取 Aspose 产品的免费试用。此许可证非常适合探索 Aspose.Page for .NET 的开发人员或软件测试人员。

最终想法

总之,使用 Aspose.Page for .NET 在 C# 中将 XPS 转换为 BMP 非常简单。探索 Aspose.Page for .NET 以了解更多关于如何 在 C# 中将 XPS 转换为 BMP 的信息。

有用的资源

其他资源,如文档和社区论坛,可以帮助增强您对 Aspose.Page for .NET 的理解。

发现更多