How to clone an image?

I have an image. I need to make an exact copy of it and save it in BufferedImage, but there is no Image.clone (). The thing should be inside the calculation loop, and therefore it should be very fast, without pixel copying. What is the best thing about this method?

+6
source share
3 answers

You can draw on a buffered image, so make an empty bufferedImage , create a graphic context from it and draw your original image on it.

BufferedImage copyOfImage = new BufferedImage(widthOfImage, heightOfImage, BufferedImage.TYPE_INT_RGB); Graphics g = copyOfImage.createGraphics(); g.drawImage(originalImage, 0, 0, null); 
+7
source

There is another way:

 BufferedImage copyOfImage = image.getSubimage(0, 0, image.getWidth, image.getHeight); 
0
source

Image clone = original.getScaledInstance(original.getWidth(), -1, Image.SCALE_DEFAULT);

It may not be very pretty, but getScaledInstance returns, as the name suggests, an instance of your original Image object. Usually used only for resizing. -1 indicates a way to keep aspect ratio as

0
source

Source: https://habr.com/ru/post/905983/


All Articles