Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/013c207eaf0c3750e9c31c38a8d14100 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/013c207eaf0c3750e9c31c38a8d14100 to your computer and use it in GitHub Desktop.
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