Search code examples
javabufferedimagegraphics2d

How to inset a image into a background and save it in JAVA?


I want to inset a image into a background and save it, both are png files with transparency, below code work fine but new image become black & white only.

BufferedImage BUFFEREDIMAGE1=ImageIO.read(new File(strPATH+"/IMAGE.png"));
BufferedImage BUFFEREDIMAGE2=ImageIO.read(new File(strPATH+"/WATERMARK.png"));
Graphics2D GRAPHICS1=BUFFEREDIMAGE1.createGraphics();
GRAPHICS1.drawImage(BUFFEREDIMAGE2,intLeft,intTop,intWidth,intHeight,null);
GRAPHICS1.dispose();
ImageIO.write(BUFFEREDIMAGE2,"png",new File(strPATH,"SAVED.png"));

Solution

  • The most likely cause is that at some point the colour space of the image is becoming changed. You may be better off explicitly creating a new destination BufferedImage with the RGB or RGBA format and writing both source Images into it. This removes any possible variation in that area.