How to get scaled instance of a bufferedImage

落花浮王杯 提交于 2019-12-03 07:20:23

You can create a new image, a BufferedImage with the TookitImage.

Image toolkitImage = img.getScaledInstance(getWidth(), getHeight(), 
      Image.SCALE_SMOOTH);
int width = toolkitImage.getWidth(null);
int height = toolkitImage.getHeight(null);

// width and height are of the toolkit image
BufferedImage newImage = new BufferedImage(width, height, 
      BufferedImage.TYPE_INT_ARGB);
Graphics g = newImage.getGraphics();
g.drawImage(toolkitImage, 0, 0, null);
g.dispose();

// now use your new BufferedImage
MadProgrammer

BufferedImage#getScaledInstance is actually inherited from java.awt.Image and only guarantees that it will return an Image so I would say it's not a good idea to try and assume the underlying return type in this case.

getScaledInstance is, also, not normally the fastest or best quality method

To scale a BufferedImage itself, you have a number of different options, but most simply take the original and repaint it to another image, applying some kind of scaling in process.

For example:

For more details about getScaledInstance, have a read of The Perils of Image.getScaledInstance()

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