การเพิ่มลายน้ำลงในภาพ

การเพิ่มลายน้ำลงในภาพ

เอกสารนี้อธิบายวิธีการเพิ่มลายน้ำลงในภาพโดยใช้ Aspose.PSD การเพิ่มลายน้ำลงในภาพเป็นความต้องการที่สามารถพบเห็นในแอปพลิเคชันประมวลผลภาพ ตัวอย่างนี้ใช้คลาส Graphics เพื่อวาดสตริงบนพื้นผิวภาพ

การเพิ่มลายน้ำ

เพื่อสาธิตการดำเนินการ จะโหลดภาพ BMP จากดิสก์และวาดสตริงเป็นลายน้ำบนพื้นผิวภาพโดยใช้เมธอด DrawString ของคลาส Graphics เราจะบันทึกภาพเป็นรูปแบบ PNG โดยใช้คลาส PngOptions ด้านล่างเป็นตัวอย่างโค้ดที่สาธิตวิธีการเพิ่มลายน้ำลงในภาพ โค้ดตัวอย่างถูกแบ่งเป็นส่วนเพื่อทำให้ง่ายต่อการติดตาม ตามลำดับ ตัวอย่างแสดงวิธีการ:

  1. โหลด ภาพ
  2. สร้างและกำหนดค่าวัตถุ Graphics
  3. สร้างและกำหนดค่า Font และวัสดุ SolidBrush
  4. วาดสตริงเป็นลายน้ำโดยใช้เมธอด DrawString ของคลาส Graphics
  5. บันทึกภาพเป็น 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 + "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.
using (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.Alignment = StringAlignment.Center;
sf.LineAlignment = 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.Width, psdImage.Height), sf);
}
// Export the image into PNG file format.
psdImage.Save(dataDir + "AddWatermark_output.png", new PngOptions());
}

การเพิ่มลายน้ำแนวเส้นทแยง

การเพิ่มลายน้ำแนวเส้นทแยงในภาพคล้ายกับการเพิ่มลายน้ำแนวนอนที่พูดถึงข้างต้น โดยมีความแตกต่างบางที ในการสาธิตการดำเนินการ เราจะโหลดภาพ JPG จากดิสก์ เพิ่มการแปลงโดยใช้วัตถุคลาส Matrix และวาดสตริงเป็นลายน้ำบนพื้นผิวภาพโดยใช้เมธอด DrawString ของคลาส Graphics ด้านล่างเป็นตัวอย่างโค้ดที่สาธิตวิธีการเพิ่มลายน้ำแนวเส้นทแยงในภาพ โค้ดตัวอย่างถูกแบ่งเป็นส่วนเพื่อทำให้ง่ายต่อการติดตาม ตามลำดับ ตัวอย่างแสดงวิธีการ:

  1. โหลดภาพ
  2. สร้างและกำหนดค่าวัตถุ Graphics
  3. สร้างและกำหนดค่า Font และ SolidBrush
  4. รับขนาดของภาพในวัตถุ SizeF
  5. สร้างอินสแตนซ์ของคลาส Matrix และดำเนินการแปลง
  6. กำหนดการแปลงให้กับวัตถุ Graphics
  7. สร้างและกำหนดค่าวัตถุ StringFormat
  8. วาดสตริงเป็นลายน้ำโดยใช้เมธอด DrawString ของคลาส Graphics
  9. บันทึกรูปภาพที่ได้

โค้ดชุดต่อไปนี้แสดงวิธีการเพิ่มลายน้ำแนวเส้นทแยง

// 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 + "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.
using (SolidBrush brush = new SolidBrush(Color.FromArgb(50, 128, 128, 128)))
{
// specify transform matrix to rotate watermark.
graphics.Transform = new Matrix();
graphics.Transform.RotateAt(45, new PointF(psdImage.Width / 2, psdImage.Height / 2));
// Specify string alignment to put watermark at the image center.
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
// Draw watermark using font, partly-transparent brush at the image center.
graphics.DrawString("Some watermark text", font, brush, new RectangleF(0, psdImage.Height / 2, psdImage.Width, psdImage.Height / 2), sf);
}
// Export the image into PNG file format.
psdImage.Save(dataDir + "AddDiagnolWatermark_output.png", new PngOptions());
}