ป้องกันการชะลอความเร็วเมื่อวาดภาพบนภาพที่ถูกบีบอัด

ป้องกันการชะลอความเร็วเมื่อวาดภาพบนภาพที่ถูกบีบอัด

มักมีเวลาที่คุณต้องการดำเนินการกราฟิกอย่างเหนือจากภาพที่ถูกบีบอัดอย่างมาก ขณะที่ Aspose.PSD ต้องบีบอัดและถอดรหัสภาพในขณะเดียวกัน การชะลอความเร็วอาจเกิดขึ้น การวาดบนภาพที่ถูกบีบอัดอาจมีความมีผลกระทบต่อความเร็วด้วย

วิธีการแก้ปัญหา

เพื่อป้องกันการชะลอความเร็ว เราขอแนะนำให้คุณแปลงภาพเป็นรูปแบบที่ไม่ถูกบีบอัดหรือแบบ raw ก่อนทำการกราฟิกอย่างเป็นพิเศษ

ใช้เส้นทางไฟล์

ในตัวอย่างที่ตามมานี้ ภาพ PSD ถูกแปลงเป็นรูปแบบ raw (ไม่บีบอัด) และบันทึกลงบนดิสก์ ภาพที่ไม่ถูกบีบอัดจากนั้นถูกโหลดกลับมาก่อนที่จะดำเนินการกราฟิกบนมัน วิธีเดียวกันใช้สำหรับไฟล์ BMP และ GIF

String dataDir = Utils.getDataDir(UncompressedImageUsingFile.class) + "PSD/";
// Load a PSD file as an image and cast it into PsdImage
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) {
PsdOptions saveOptions = new PsdOptions();
saveOptions.setCompressionMethod(CompressionMethod.Raw);
psdImage.save(dataDir + "uncompressed_out.psd", saveOptions);
// Now reopen the newly created image.
PsdImage img = (PsdImage) Image.load(dataDir + "uncompressed_out.psd");
Graphics graphics = new Graphics(img);
// Perform graphics operations.
}

ใช้วัตถุสตรีม

โค้ดตัวอย่างต่อไปนี้แสดงวิธีการแปลงภาพ PSD เป็นรูปแบบ raw (ไม่บีบอัด) และบันทึกลงบนดิสก์ โดยใช้วัตถุสตรีม

String dataDir = Utils.getDataDir(UncompressedImageStreamObject.class) + "PSD/";
ByteArrayOutputStream ms = new ByteArrayOutputStream();
// Load a PSD file as an image and cast it into PsdImage
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) {
PsdOptions saveOptions = new PsdOptions();
saveOptions.setCompressionMethod(CompressionMethod.Raw);
psdImage.save(ms, saveOptions);
// Now reopen the newly created image. But first seek to the beginning of stream since after saving seek is at the end now.
ms.reset();
PsdImage img = (PsdImage) Image.load(new ByteArrayInputStream(ms.toByteArray()));
Graphics graphics = new Graphics(psdImage);
// Perform graphics operations.
}