3

I have a BufferedImage. I want to do two copy of it, to write some things with their Graphics. But, if I write something on one, it will do the same on all the other copy, so I tried to do image.getSubImage(0, 0, image.getWidth(), image.getHeight());, but it doesn't change anything.

I don't know what to do, it would be super to help me.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Litarvan
  • 125
  • 3
  • 9
  • You should show us your code, it is probably a quick fix – Dici Jul 02 '15 at 12:53
  • This is an entire library, so it contains so many classes, sinclair's solution works ! I just wait because i can't valid the solution now. – Litarvan Jul 02 '15 at 12:57
  • `image.getSubImage(0, 0, image.getWidth(), image.getHeight()` just returns image, not a clone. – MCMastery Jul 02 '15 at 12:57
  • @TheShark34 You can upvote it – MCMastery Jul 02 '15 at 12:57
  • @TheShark34 you didn't need to link your whole project.... just the relevant lines would have been ok – Dici Jul 02 '15 at 13:07
  • @TheShark34 sinclair does not provide a solution, he (assumption) only provides an existing question with answer which apparently answers yours. In other words: this is a duplicate question. As such I voted to close as a duplicate. – Gimby Jul 02 '15 at 13:27
  • @MCMastery Oh ok, i can't upvote it because i don't have enough reputation, and the question that he given had a working solution, and the lines was using so much methods and things, so this is why i didn't give the code. – Litarvan Jul 02 '15 at 14:14

2 Answers2

3

Take a look at the top answer of this question and see if it fits your situation:

How do you clone a BufferedImage

Community
  • 1
  • 1
sinclair
  • 2,812
  • 4
  • 24
  • 53
  • Omg it works ! I don't know why i didn't found this solution, thanks you so much ! I just need to wait a little to accept the answer, but thanks you so much ! – Litarvan Jul 02 '15 at 12:58
1

Please, try something like this:

ColorModel model = image.getColorModel();
WritableRaster raster = image.copyData(null);
BufferedImage clone = new BufferedImage(model, raster, model.isAlphaPremultiplied(), null);
vojta
  • 5,591
  • 2
  • 24
  • 64