java - How to save an image in a set location? -
i trying save resized picture user's desktop not sure how that.
here's code far:
mi.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { string userhome = system.getproperty("user.home"); filechooser = new jfilechooser(userhome + "\\desktop"); filechooser.setautoscrolls(true); switch (filechooser.showopendialog(f)) { case jfilechooser.approve_option: bufferedimage img = null; try { img = imageio.read(filechooser.getselectedfile()); } catch (ioexception e1) { e1.printstacktrace(); } image dimg = img.getscaledinstance(f.getwidth(), f.getheight(), image.scale_smooth); path = new imageicon(dimg); configprops.setproperty("path", filechooser .getselectedfile().getpath()); imbg.seticon(path); break; } } });
the code above resizes imaged selected fit size of jframe
sets jlabel
.
this works want output file set location lets users desktop make easier. i'm looking @ output stream can't quite head around it.
any great.
get current icon
jlabel
...
icon icon = imgbg.geticon();
paint icon bufferedimage
...
bufferedimage img = new bufferedimage(icon.geticonwidth(), icon.geticonheight(), bufferedimage.type_int_argb); graphics2d g2d = img.creategraphics(); icon.painticon(null, g2d, 0, 0); g2d.dispose();
save image file...
imageio.write(img, "png", new file("resizedicon.png"));
(and yes, use jfilechooser
pick file location/name)
you should take @ this better examples of scaling image, way, scale bufferedimage
bufferedimage
, save hassle of having re-paint icon
you might take @ writing/saving image