Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Created November 2, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/3a16593b4b5a6b91be96055a1bdee5a2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/3a16593b4b5a6b91be96055a1bdee5a2 to your computer and use it in GitHub Desktop.
Convert XPS OXPS to JPG PNG Image Programmatically in C# VB.NET
// 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);
}
}
// 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment