1

I have 2 BufferedImage that I want to place one on top of the other(layers like), here is my code following combine-buffered-images:

private void combainImages(List<String> imageList, String combainedImages) throws IOException {
    BufferedImage a = ImageIO.read(new File(imageList.get(0))); //imageList holds the path to all images
    BufferedImage b = ImageIO.read(new File(imageList.get(1)));
    BufferedImage c = new BufferedImage(a.getWidth(), a.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics g = c.getGraphics();
    g.drawImage(a, 0, 0, null);
    g.drawImage(b, 0, 0, null);
    ImageIO.write(c, "PNG", new File(combainedImages));

}

The result is that the second image is draw on the first image not beneath, so in combainedImages is same as the second picture.

Thanks for any help.

Itsik Mauyhas
  • 3,824
  • 14
  • 69
  • 114
  • so the fix is `g.drawImage(b, a.getWidth(), a.getHeight(), null);`? – Itsik Mauyhas Jun 27 '17 at 08:46
  • what did you expect? you first drew a, and then b. So b drew over a – Tom K Jun 27 '17 at 09:39
  • So, if you want to layer the image, one on top of the other, what's wrong with this approach? This should do just that. I'm afraid I'm not sure what it is you're actually trying to achieve. Perhaps you should photoshop something to demonstrate your desired result – MadProgrammer Jun 27 '17 at 11:09
  • @MadProgrammer - I dont want to draw one picture to cover the other one, I want 2 have both pictures one beneath the other. – Itsik Mauyhas Jun 28 '17 at 12:29
  • Right, stop, think about what you're trying to do, maybe even grab a couple of pieces of paper so you have a physical model to work with. You need to create an image which is large enough to contain the two images you have, you then need to draw one image at `0x0` (top/left) and one below, offset by the height of the first – MadProgrammer Jun 29 '17 at 01:04

0 Answers0