Resize an image without reading it again from disk?

笑着哭i 提交于 2019-12-09 01:03:28

all you need is ImageView

load image one time in memory ,and then do manipulation on imageview when ever u need .

Sample Code :

//load image to memory
final static Image MY_IMAGE = new Image(path of image file);
//create different imageviews pointing to image in memory and do manipulations
ImageView myImage = new ImageView(MY_IMAGE);
myImage.setFitHeight(value);
myImage.setFitWidth(value);

invariant was close to the final solution, credits for the final solution go to l2p in the oracle forum:

ImageView does not alter the actual Image whether it is in a scene or not. It only renders a visible node from the given image. If you want it to render a new Image from its current visible state you have to make a snapshot(). So:

Image resizedImage = myImageView.snapshot(null, null); // after using myImageView.setFitHeight()/setFitWidth/( etc.

Bugfix:

Transparency is lost with the current approach, because default control (herE: imageview) background is white. screenshot of transparency (from our image) on white gives white, so we loose the original transparency. fix for this: set the background itself to transparent.

So, the fixed code:

SnapshotParameters params = new SnapshotParameters();
params.setFill(Color.TRANSPARENT); 
return imageView.snapshot(params, null); 

Hell, tricky this stuff ;-)

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