إضافة علامة مائية لصورة
إضافة علامة مائية لصورة
هذا المستند يشرح كيفية إضافة علامة مائية إلى صورة باستخدام Aspose.PSD. إضافة علامة مائية إلى صورة هو متطلب شائع في تطبيقات معالجة الصور. يستخدم هذا المثال فئة الرسومات لرسم سلسلة على سطح الصورة.
إضافة علامة مائية
لتوضيح العملية، سنقوم بتحميل صورة BMP من القرص ورسم سلسلة كعلامة مائية على سطح الصورة باستخدام طريقة DrawString في فئة الرسومات. سنقوم بحفظ الصورة بتنسيق PNG باستخدام فئة PngOptions. فيما يلي مثال على الشيفرة التي توضح كيفية إضافة علامة مائية لصورة. تم تقسيم شيفرة المصدر الخاصة بالمثال إلى أجزاء لجعل متابعتها سهلة. خطوة بخطوة، توضح الأمثلة كيفية:
- تحميل صورة.
- أنشاء وتهيئة كائن الرسومات Graphics.
- إنشاء وتهيئة فئة Font و SolidBrush.
- رسم سلسلة كعلامة مائية باستخدام طريقة DrawString في فئة الرسومات.
- حفظ الصورة بتنسيق 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 في فئة الرسومات. فيما يلي مثال على الشيفرة التي توضح كيفية إضافة علامة مائية قطرية إلى صورة. تم تقسيم شيفرة المصدر الخاصة بالمثال إلى أجزاء لجعل متابعتها سهلة. خطوة بخطوة، توضح الأمثلة كيفية:
- تحميل صورة.
- إنشاء وتهيئة كائن رسومات.
- إنشاء وتهيئة فئة الFont وكائن SolidBrush.
- الحصول على حجم الصورة في كائن SizeF.
- إنشاء نسخة من فئة الMatrix وتنفيذ تحويل مركب.
- تعيين التحويل إلى كائن الرسومات.
- إنشاء وتهيئة فئة StringFormat.
- رسم سلسلة كعلامة مائية باستخدام طريقة DrawString في فئة الرسومات.
- حفظ الصورة الناتجة.
الشيفرة التالية تظهر لك كيفية إضافة علامة مائية قطرية.
// 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()); | |
} |