إضافة علامة مائية إلى صورة

إضافة علامة مائية إلى صورة

يوضح هذا المستند كيفية إضافة علامة مائية إلى صورة باستخدام Aspose.PSD. إضافة علامة مائية إلى صورة هو متطلب شائع في تطبيقات معالجة الصور. يستخدم هذا المثال فئة Graphics لرسم سلسلة نصية على سطح الصورة.

إضافة علامة مائية

لتوضيح العملية، سنقوم بتحميل صورة BMP من القرص ورسم سلسلة نصية كعلامة مائية على سطح الصورة باستخدام طريقة DrawString من فئة Graphics. سنقوم بحفظ الصورة بتنسيق PNG باستخدام فئة PngOptions. فيما يلي مثال على الشفرة التي توضح كيفية إضافة علامة مائية إلى صورة. تم تقسيم شفرة المصدر إلى أجزاء ليسهل اتباعها خطوة بخطوة، تُظهر الأمثلة كيفية:

  1. تحميل صورة.
  2. إنشاء وتهيئة كائن Graphics.
  3. إنشاء وتهيئة كائن Font و SolidBrush.
  4. رسم سلسلة نصية كعلامة مائية باستخدام طريقة DrawString من فئة Graphics.
  5. حفظ الصورة بتنسيق 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. فيما يلي مثال على الشفرة التي توضح كيفية إضافة علامة مائية بشكل قطري إلى صورة. تم تقسيم شفرة المصدر إلى أجزاء ليسهل اتباعها خطوة بخطوة، تُظهر الأمثلة كيفية:

  1. تحميل صورة.
  2. إنشاء وتهيئة كائن Graphics.
  3. إنشاء وتهيئة كائن Font و SolidBrush.
  4. الحصول على حجم الصورة في كائن SizeF.
  5. إنشاء مثيل من فئة Matrix وتنفيذ تحويل تكميلي.
  6. تعيين التحويل إلى كائن Graphics.
  7. إنشاء وتهيئة كائن StringFormat.
  8. رسم سلسلة نصية كعلامة مائية باستخدام طريقة DrawString من فئة Graphics.
  9. حفظ الصورة الناتجة.

الكود التالي يُظهر لك كيفية إضافة علامة مائية بشكل قطري.

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());
}