הוספת סימן מים לתמונה

מסמך זה מסביר איך להוסיף סימן מים לתמונה באמצעות 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());
}