การจัดการกับรูปภาพ PNG
ระบุความโปร่งใสสำหรับรูปภาพ PNG
หนึ่งในข้อดีของการบันทึกรูปภาพในรูปแบบ PNG คือ PNG สามารถมีพื้นหลังโปร่งใสได้ Aspose.PSD สำหรับ .NET มีคุณสมบัติในการระบุความโปร่งใสสำหรับรูปภาพ PNG และรูปภาพแบบเรสเตอร์เช่นที่แสดงในส่วนด้านล่างนี้ Aspose.PSD สำหรับ .NET API สามารถใช้เพื่อตั้งค่าสีใดก็ได้ให้โปร่งใสขณะสร้างรูปภาพ PNG ใหม่หรือแปลงรูปภาพที่มีอยู่เป็นรูปแบบ PNG เพื่อวัตถุประสงค์เหล่านั้น Aspose.PSD สำหรับ .NET API ได้เปิดเผยคุณสมบัติ TransparentColor และการจำแนกประเภทสีของ PngColorType ที่สามารถตั้งได้เพื่อระบุสีใดเป็นทรานสปาร์เรนต์ในภาพ PNG โค้ดย่อยที่ให้ไว้ด้านล่างแสดงตัวอย่างการแปลงภาพ PSD ที่มีอยู่เป็นภาพ PNG โดยระบุสีที่ต้องการให้โปร่งใส
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// specify the PNG image transparency options and save to file. | |
psdImage.TransparentColor = Color.White; | |
psdImage.HasTransparentColor = true; | |
PngOptions opt = new PngOptions(); | |
psdImage.Save(dataDir + "Specify_Transparency_result.png", new PngOptions()); | |
} |
กำหนดความละเอียดสำหรับรูปภาพ PNG
Aspose.PSD สำหรับ .NET เปิดเผยคลาส ResolutionSetting ซึ่งสามารถใช้เพื่อกำหนดความละเอียดสำหรับภาพทุกประเภทรวมทั้ง PNG บทความนี้สาธิตการใช้ Aspose.PSD สำหรับ .NET API เพื่อกำหนดพารามิเตอร์ความละเอียดแนวนอนและแนวตั้งสำหรับรูปภาพ PNG โค้ดย่อยด้านล่างโหลดภาพ PSD ที่มีอยู่และแปลงเป็นรูปแบบ PNG โดยเปลี่ยนความละเอียด
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the horizontal & vertical resolutions and Save the result on disc | |
PngOptions options = new PngOptions(); | |
options.ResolutionSettings = new ResolutionSetting(72, 96); | |
psdImage.Save(dataDir + "SettingResolution_output.png", options); | |
} |
บีบอัดไฟล์ PNG
Portable Network Graphic (PNG) เป็นรูปแบบการบีบอัดที่ไม่สูญเสียสำหรับการส่งข้อมูลในรูปภาพบิทแมพผ่านเครือข่าย เมื่อคุณบันทึกรูปภาพเป็นไฟล์ PNG ในโปรแกรมใดๆ คุณอาจถูกถามให้เลือกระดับการบีบอัดในช่วงตั้งแต่ 0 ถึงระดับสูงสุด การกำหนดค่านี้จริงๆ แล้วจะบีบอัดขนาดไฟล์และไม่ลดคุณภาพของภาพบทความนี้อธิบายถึงว่า Aspose.PSD APIs ช่วยให้คุณควบคุมขนาดไฟล์ PNG ได้ Aspose.PSD APIs สามารถใช้เพื่อกำหนดระดับการบีบอัดสำหรับรูปแบบไฟล์ PNG โดยใช้คลาส PngOptions ที่มี CompressionLevel อยู่ในรูปแบบ int property คุณสมบัตินี้ยอมรับค่าตั้งแต่ 0 ถึง 9 โดยที่ 9 คือการบีบอัดสูงสุด ตัวอย่างโค้ดย่อยที่ให้ด้านล่างDemo วิธีการตั้งค่าระดับการบีบอัดโดยใช้ Aspose.PSD สำหรับ .NET API
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Loop over possible CompressionLevel range | |
for (int i = 0; i <= 9; i++) | |
{ | |
// Create an instance of PngOptions for each resultant PNG, Set CompressionLevel and Save result on disk | |
PngOptions options = new PngOptions(); | |
options.CompressionLevel = i; | |
psdImage.Save(dataDir + i + "_out.png", options); | |
} | |
} |
ระบุความลึกของบิตสำหรับรูปภาพ PNG
ความลึกของบิตในการภาพถ่ายคือจำนวนของบิตที่ใช้เพื่อแสดงสีของพิกเซลเดี่ยวในรูปภาพบิตแมพเหมือนกับรูปภาพ BMP ความลึกสี PNG ก็ถูกแทนด้วยบิต เช่น 1 บิต (2 สี) 2 บิต (4 สี) 4 บิต (16 สี) และ 8 บิต (256 สี) Aspose.PSD สำหรับ .NET API สามารถใช้กำหนดความลึกของบิตสำหรับรูปภาพ PNG โดยใช้ BitDepth property ที่เปิดเผยโดยคลาส PngOptions ในปัจจุบัน คุณสมบัติ BitDepth สามารถถูกตั้งค่าได้ที่ 1, 2, 4 หรือ 8 บิตสำหรับระดับสีเทาและสีดัชรี สำหรับสีชนิดอื่น ๆ เท่านั้นมีการสนับสนุน 8 บิตเท่านั้น ตัวอย่างโค้ดย่อยที่ให้ไว้ด้านล่างแสดงวิธีการตั้งค่าความลึกของบิตสำหรับภาพ PNG
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the desired ColorType, BitDepth according to the specified ColorType and save image | |
PngOptions options = new PngOptions(); | |
options.ColorType = PngColorType.Grayscale; | |
options.BitDepth = 1; | |
psdImage.Save(dataDir + "SpecifyBitDepth_out.png", options); | |
} |
ปรับใช้วิธีกรองลำดับในรูปภาพ PNG
ความลึกของบิตในการภาพถ่ายคือจำนวนของบิตที่ใช้เพื่อแสดงสีของพิกเซลเดี่ยวในรูปภาพบิตแมพเหมือนกับรูปภาพ BMP ความลึกสี PNG ก็ถูกแทนด้วยบิต เช่น 1 บิต (2 สี) 2 บิต (4 สี) 4 บิต (16 สี) และ 8 บิต (256 สี) Aspose.PSD สำหรับ .NET API สามารถใช้กำหนดความลึกของบิตสำหรับรูปภาพ PNG โดยใช้ BitDepth property ที่เปิดเผยโดยคลาส PngOptions ในปัจจุบัน คุณสมบัติ BitDepth สามารถถูกตั้งค่าได้ที่ 1, 2, 4 หรือ 8 บิตสำหรับระดับสีเทาและสีดัชรี สำหรับสีชนิดอื่น ๆ เท่านั้นมีการสนับสนุน 8 บิตเท่านั้น ตัวอย่างโค้ดย่อยที่ให้ไว้ด้านล่างแสดงวิธีการตั้งค่าความลึกของบิตสำหรับภาพ PNG
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
// Create an instance of PngOptions, Set the PNG filter method and Save changes to the disc | |
PngOptions options = new PngOptions(); | |
options.FilterType = PngFilterType.Paeth; | |
psdImage.Save(dataDir + "ApplyFilterMethod_out.png", options); | |
} |
เปลี่ยนสีพื้นหลังของภาพ PNG โปร่งใส
ภาพในรูปแบบ PNG สามารถมีพื้นหลังโปร่งใส Aspose.PSD สำหรับ .NET มีคุณสมบัติในการเปลี่ยนสีพื้นหลังของภาพ PNG ที่มีพื้นหลังโปร่งใส Aspose.PSD สำหรับ .NET API สามารถใช้เพื่อตั้ง/เปลี่ยนสีของภาพ PNG ที่มีพื้นหลังโปร่งใสตัวอย่างโค้ดย่อยที่ให้ไว้ด้านล่างแสดงวิธีการตั้ง/เปลี่ยนสีพื้นหลังของภาพ PNG
// For complete examples and data files, please go to https://github.com/aspose-psd/Aspose.PSD-for-.NET | |
// Load a PSD file as an image and cast it into PsdImage | |
using (PsdImage psdImage = (PsdImage)Image.Load(dataDir + "sample.psd")) | |
{ | |
int[] pixels = psdImage.LoadArgb32Pixels(psdImage.Bounds); | |
// Iterate through the pixel array and Check the pixel information | |
//that if it is a transparent color pixel and Change the pixel color to white | |
int transparent = psdImage.TransparentColor.ToArgb(); | |
int replacementColor = Color.Yellow.ToArgb(); | |
for (int i = 0; i < pixels.Length; i++) | |
{ | |
if (pixels[i] == transparent) | |
{ | |
pixels[i] = replacementColor; | |
} | |
} | |
// Replace the pixel array into the image. | |
psdImage.SaveArgb32Pixels(psdImage.Bounds, pixels); | |
psdImage.Save(dataDir + "ChangeBackground_out.png", new PngOptions()); | |
} |