Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem must lie in your <code>resizeImage</code> method. I have no issue with your code:</p> <pre><code>import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class TestImageResize { protected void initUI() throws MalformedURLException, IOException { final JFrame frame = new JFrame(TestImageResize.class.getSimpleName()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BufferedImage bi = ImageIO.read(new URL("http://noupe.com/img/wallpaper-4.jpg")); File file = new File(System.getProperty("user.home"), "test.jpg"); FileOutputStream fos = new FileOutputStream(file); Image image = bi.getScaledInstance(100, -1, Image.SCALE_SMOOTH); BufferedImage bsi = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB); bsi.getGraphics().drawImage(image, 0, 0, null); ImageIO.write(bsi, "jpg", fos); JPanel panel = new JPanel(); JLabel label = new JLabel(new ImageIcon(file.getAbsolutePath())); panel.add(label); frame.add(panel); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { new TestImageResize().initUI(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } } </code></pre> <p>Result scaled image displayed in JFrame: <img src="https://i.stack.imgur.com/P7xFc.png" alt="Result scaled image"></p> <p>Result scaled image (displayed by your browser): <img src="https://i.stack.imgur.com/NgHQy.jpg" alt="Result image uploaded from disk"></p> <p>Original image: <img src="https://i.stack.imgur.com/tdI9n.jpg" alt="Original image"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload