Convert XPS to BMP in C#
Converting XPS files to BMP images in C# is a common requirement when you need to extract visual representations of XPS documents for printing, archiving, or integrating into image processing workflows. Using Conholdate.Total for .NET, this task becomes straightforward and efficient.
Why Convert XPS to BMP?
Here are some reasons you might need to convert XPS to BMP:
- High-quality output: BMP preserves full image fidelity, useful for printing or design work.
- Simplified sharing: BMP images can be easily opened and viewed across platforms.
- Data extraction: Extract page-level content for analysis or OCR.
- Batch processing: Convert multiple pages or documents in one go using .NET automation.
Convert XPS to BMP in C#
Follow the steps below to perform the conversion:
- Load the XPS document using the
XpsDocumentclass. - Create and configure an instance of
BmpSaveOptions. - Invoke the
SaveAsImagemethod to generate BMP image byte arrays. - Iterate through each page and save the images to the disk.
Convert XPS to BMP in 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.
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);
}
}
This code demonstrates a complete process for converting XPS documents to BMP images in C#. The XPS file is loaded using the XpsDocument class along with XpsLoadOptions to prepare it for rendering. Then, a BmpSaveOptions object is initialized, where you can set the image resolution, smoothing mode, and specific pages to be converted. This ensures that every page is saved separately with clear labeling, making it easy to manage and use the generated images in further applications. Overall, this method provides a highly efficient way to extract and preserve XPS content in the BMP format.
Conclusion
Converting XPS documents to BMP images in C# allows developers to generate rasterized image outputs for easy integration, archiving, or further processing. Conholdate.Total for .NET offers powerful APIs to handle such document conversions with minimal effort.
