การเพิ่มลายน้ำลงในรูปภาพ
การเพิ่มลายน้ำลงในรูปภาพ
เอกสารนี้อธิบายถึงวิธีการเพิ่มลายน้ำลงในรูปภาพโดยใช้ Aspose.PSD การเพิ่มลายน้ำลงในรูปภาพเป็นความต้องการที่พบบ่อยสำหรับแอปพลิเคชันประมวลผลภาพ ตัวอย่างนี้ใช้คลาส Graphics เพื่อวาดสตริงบนพื้นผิวรูปภาพ
การเพิ่มลายน้ำ
เพื่อสาธิตการดำเนินการ เราจะโหลดรูปภาพ BMP จากดิสก์และวาดสตริงเป็นลายน้ำบนพื้นผิวรูปภาพโดยใช้วิธี DrawString ของคลาส Graphics เราจะบันทึกรูปภาพเป็นรูปแบบ PNG โดยใช้คลาส PngOptions ด้านล่างเป็นตัวอย่างโค้ดที่แสดงวิธีการเพิ่มลายน้ำลงในรูปภาพ โค้ดตัวอย่างถูกแบ่งเป็นส่วนเพื่อทำให้ง่ายต่อการติดตาม ขั้นตอนต่อขั้น ตัวอย่างแสดงวิธีการ:
- โหลดรูปภาพ
- สร้างและกำหนดค่าวัตถุ Graphics
- สร้างและกำหนดค่า Font และ SolidBrush
- วาดสตริงเป็นลายน้ำโดยใช้วิธี DrawString ของคลาส Graphics
- บันทึกรูปภาพเป็น PNG
ส่วนโค้ดต่อไปนี้แสดงให้เห็นวิธีการเพิ่มลายน้ำในรูปภาพ.
String dataDir = Utils.getDataDir(AddWatermark.class) + "PSD/"; | |
// Load a PSD file as an image and cast it into PsdImage | |
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) { | |
// Create graphics object to perform draw operations. | |
Graphics graphics = new Graphics(psdImage); | |
// Create font to draw watermark with. | |
Font font = new Font("Arial", 20.0f); | |
// Create a solid brush with color alpha set near to 0 to use watermarking effect. | |
SolidBrush brush = new SolidBrush(Color.fromArgb(50, 128, 128, 128)); | |
// Specify string alignment to put watermark at the image center. | |
StringFormat sf = new StringFormat(); | |
sf.setAlignment(StringAlignment.Center); | |
sf.setLineAlignment(StringAlignment.Center); | |
// Draw watermark using font, partly-transparent brush and rotation matrix at the image center. | |
graphics.drawString("Some watermark text", font, brush, new RectangleF(0, 0, psdImage.getWidth(), psdImage.getHeight()), sf); | |
// Export the image into PNG file format. | |
psdImage.save(dataDir + "AddWatermark_output.png", new PngOptions()); | |
} |
การเพิ่มลายน้ำแนวเฉียง
การเพิ่มลายน้ำแนวเฉียงในรูปภาพคล้ายกับการเพิ่มลายน้ำแนวนอนที่อธิบายไว้ข้างต้น โดยมีความแตกต่างเล็กน้อย เพื่อสาธิตการดำเนินการ เราจะโหลดรูปภาพ JPG จากดิสก์ เพิ่มการแปลงใช้วัตถุของคลาส Matrix และวาดสตริงเป็นลายน้ำบนพื้นผิวรูปภาพโดยใช้วิธี DrawString ของคลาส Graphics ตัวอย่างโค้ดถูกแบ่งเป็นส่วนเพื่อทำให้ง่ายต่อการติดตาม ขั้นตอนต่อขั้น ตัวอย่างแสดงวิธีการ:
- โหลดรูปภาพ
- สร้างและกำหนดค่าวัตถุ Graphics
- สร้างและกำหนดค่า Font และ SolidBrush
- รับขนาดของรูปภาพในวัตถุ SizeF
- สร้างอินสแตนซ์ของคลาส Matrix และดำเนินการแปลงทวนสถานะ
- กำหนดการแปลงให้กับวัตถุ Graphics
- สร้างและกำหนดค่าวัตถุ StringFormat
- วาดสตริงเป็นลายน้ำโดยใช้วิธี DrawString ของคลาส Graphics
- บันทึกรูปภาพที่ได้
ส่วนโค้ดต่อไปนี้แสดงให้เห็นวิธีการเพิ่มลายน้ำแนวเฉียง.
String dataDir = Utils.getDataDir(AddDiagnolWatermark.class) + "PSD/"; | |
// Load a PSD file as an image and cast it into PsdImage | |
try (PsdImage psdImage = (PsdImage) Image.load(dataDir + "layers.psd")) { | |
// Create graphics object to perform draw operations. | |
Graphics graphics = new Graphics(psdImage); | |
// Create font to draw watermark with. | |
Font font = new Font("Arial", 20.0f); | |
// Create a solid brush with color alpha set near to 0 to use watermarking effect. | |
SolidBrush brush = new SolidBrush(Color.fromArgb(50, 128, 128, 128)); | |
// specify transform matrix to rotate watermark. | |
graphics.setTransform(new Matrix()); | |
graphics.getTransform().rotateAt(45, new PointF(psdImage.getWidth() / 2, psdImage.getHeight() / 2)); | |
// Specify string alignment to put watermark at the image center. | |
StringFormat sf = new StringFormat(); | |
sf.setAlignment(StringAlignment.Center); | |
// Draw watermark using font, partly-transparent brush at the image center. | |
graphics.drawString("Some watermark text", font, brush, new RectangleF(0, psdImage.getHeight() / 2, psdImage.getWidth(), psdImage.getHeight() / 2), sf); | |
// Export the image into PNG file format. | |
psdImage.save(dataDir + "AddDiagnolWatermark_output.png", new PngOptions()); | |
} |