How do i save a resized image to a specific folder?
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
ImgChooser ic = new ImgChooser();
ImageIcon icon = new ImageIcon(me,"id pic");
Image img1 = icon.getImage();
Image img2 = img1.getScaledInstance(105, 105, 0);
icon.setImage(img2);
jLabel1.setIcon(icon);
}
This first code is where i get the image and resize it. Then i want the resized image to be saved in another folder. Thanks in advance
Use ImageIO.write(...)
as others have already said (+1 to them), to add here is an example for you:
public static void main(String[] args) {
try {
BufferedImage originalImage = ImageIO.read(new File("c:\\test.jpg"));//change path to where file is located
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizeImageJpg = resizeImage(originalImage, type, 100, 100);
ImageIO.write(resizeImageJpg, "jpg", new File("c:\\images\\testresized.jpg")); //change path where you want it saved
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
Reference:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With