How to clone Image?

我只是一个虾纸丫 提交于 2019-11-30 20:41:20
Levster

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);

There is another way:

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!