How to clone Image?

2019-02-13 13:13发布

I have an Image. I need to make a exactly copy of it and save it to BufferedImage, but there is no Image.clone(). The thing should be inside a calculating loop and so it should be really fast, no pixel-by-pixel copying. What's the best in perfomance method to do this?

3条回答
我只想做你的唯一
2楼-- · 2019-02-13 13:40

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

This might not be very pretty, but getScaledInstance returns, as the name suggests, an instance of your original Image object. Usually only used for resizing. -1 tells the method to keep the aspect ratio as it is

查看更多
来,给爷笑一个
3楼-- · 2019-02-13 13:47

You can draw to a buffered image, so make a blank bufferedImage, create a graphics context from it, and draw your original image to it.

BufferedImage copyOfImage = 
   new BufferedImage(widthOfImage, heightOfImage, BufferedImage.TYPE_INT_RGB);
Graphics g = copyOfImage.createGraphics();
g.drawImage(originalImage, 0, 0, null);
查看更多
一夜七次
4楼-- · 2019-02-13 13:57

There is another way:

BufferedImage copyOfImage = image.getSubimage(0, 0, image.getWidth, image.getHeight);
查看更多
登录 后发表回答