Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Watermark to Images in Java
// Load image
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("image.png");
// Create and initialize an instance of Graphics class
Graphics graphics= new Graphics(image);
// Creates an instance of Font
Font font = new Font("Times New Roman", 16, FontStyle.Bold);
// Create an instance of SolidBrush and set its properties
SolidBrush brush = new SolidBrush();
brush.setColor(Color.getBlack());
brush.setOpacity(100);
Size sz = graphics.getImage().getSize();
// Create an object of Matrix class for transformation
Matrix matrix = new Matrix();
// First a translation then a rotation
matrix.translate(sz.getWidth() / 2, sz.getHeight() / 2);
matrix.rotate(-45.0f);
// Set the Transformation through Matrix
graphics.setTransform(matrix);
// Draw a string using the SolidBrush and Font objects at specific point
graphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, 0, 0);
// Save image
image.save("watermarked-image.png");
// Load image
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("image.png");
// Create and initialize an instance of Graphics class
Graphics graphics= new Graphics(image);
// Creates an instance of Font
Font font = new Font("Times New Roman", 16, FontStyle.Bold);
// Create an instance of SolidBrush and set its properties
SolidBrush brush = new SolidBrush();
brush.setColor(Color.getBlack());
brush.setOpacity(100);
// Draw a string using the SolidBrush and Font objects at specific point
graphics.drawString("Watermark by Aspose.Imaging for Java", font, brush, new PointF(image.getWidth()-100, image.getHeight()-100));
// Save image
image.save("watermarked-image.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment