翻譯|使用教程|編輯:李顯亮|2020-11-04 10:18:25.353|閱讀 464 次
概述:XPS文件格式經常在文件處理應用程序中使用。本文將講解在基于.NET Framework的應用程序中使用C#或VB.NET以編程方式將XPS或OXPS轉換為JPG或PNG圖像。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
XPS文件格式經常在文件處理應用程序中使用。您可以在基于.NET Framework的應用程序中使用C#或VB.NET以編程方式將XPS或OXPS轉換為JPG或PNG圖像。
讓我們逐步介紹以下方案,以了解有關XPS轉換的更多信息:
目前,.NET版Aspose.page升級到v20.10版,感興趣的朋友可點擊下方按鈕下載最新版。
(安裝包僅提供部分功能,并設置限制,如需試用完整功能請。)
JPG文件之所以出名是因為它們在所有系統(tǒng)環(huán)境中均受支持。您可以使用C#或VB.NET以編程方式將XPS或OXPS文件轉換為JPG圖像。對于XPS或OXPS到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和Aspose.Page for .NET API將XPS或OXPS文件轉換為PNG。您需要按照以下步驟將XPS轉換為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); } }
本站文章除注明轉載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn