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