แปลง Xps เป็น Bmp ใน C# โดยการเขียนโปรแกรม

ภาพรวม

การแปลง XPS เป็น BMP ใน C# เป็นสิ่งสำคัญสำหรับแอปพลิเคชันต่าง ๆ Aspose.Page สำหรับ .NET มีบทบาทสำคัญในหลายอุตสาหกรรม มันช่วยให้นักพัฒนาสามารถแปลงไฟล์ XPS เป็นรูปแบบ BMP โดยการเขียนโปรแกรม ในขณะเดียวกัน Aspose.Page สำหรับ .NET ยังทำให้กระบวนการง่ายขึ้นด้วยความสะดวกในการรวมและความยืดหยุ่น ดังนั้น มาดูวิธีการ แปลง XPS เป็น BMP ใน C# โดยการเขียนโปรแกรมกันเถอะ

หัวข้อที่จะแสดงในบทความนี้:

การติดตั้งไลบรารี C# XPS

เพื่อติดตั้ง Aspose.Page สำหรับ .NET ดาวน์โหลดได้จาก https://releases.aspose.com/page/net/ และรันคำสั่ง Install-Package Aspose.Page ไลบรารีนี้เหมาะสำหรับการแปลง XPS เป็น BMP ใน C# เนื่องจากมีตัวเลือกการปรับแต่งที่ทันสมัย

แปลง XPS เป็น BMP ใน C# - โค้ดตัวอย่าง

ในการแปลง XPS เป็น BMP ใน C# โดยใช้ Aspose.Page สำหรับ .NET ให้ทำตามขั้นตอนเหล่านี้:

  1. สร้างอินสแตนซ์ของคลาส XpsLoadOptions
  2. โหลดเอกสาร XPS จากไฟล์ XPS โดยการสร้างอินสแตนซ์ของคลาส XpsDocument
  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 - ลองออนไลน์

ลองใช้เครื่องมือแปลง XPS เป็น BMP ออนไลน์ที่พัฒนาโดย Aspose.Page สำหรับ .NET ฟรี ง่ายต่อการใช้งาน และแม่นยำ

เครื่องมือแปลงไฟล์ XPS

รับใบอนุญาตฟรี

รับ การทดลองใช้ฟรีสำหรับผลิตภัณฑ์ Aspose ใบอนุญาตนี้เหมาะสำหรับนักพัฒนาหรือผู้ทดสอบซอฟต์แวร์ที่สำรวจ Aspose.Page สำหรับ .NET

ความคิดสุดท้าย

สรุปได้ว่า การแปลง XPS เป็น BMP ใน C# นั้นง่ายมากด้วย Aspose.Page สำหรับ .NET สำรวจ Aspose.Page สำหรับ .NET เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับวิธีการ แปลง XPS เป็น BMP ใน C#

แหล่งข้อมูลที่มีประโยชน์

แหล่งข้อมูลเพิ่มเติมเช่นเอกสารและฟอรัมชุมชนสามารถช่วยเพิ่มความเข้าใจของคุณเกี่ยวกับ Aspose.Page สำหรับ .NET

ค้นพบเพิ่มเติม