39

I'm trying to make a picture fit a JLabel. I wish to reduce the picture dimensions to something more appropriate for my Swing JPanel.

I tried with setPreferredSize but it doesn't work.

I'm wondering if there is a simple way to do it? Should I scale the image for this purpose?

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • see this exmple mayby can help you [enter link description here][1] [1]: http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size – zdi301 Apr 04 '14 at 21:15

8 Answers8

79

Outline

Here are the steps to follow.

  • Read the picture as a BufferedImage.
  • Resize the BufferedImage to another BufferedImage that's the size of the JLabel.
  • Create an ImageIcon from the resized BufferedImage.

You do not have to set the preferred size of the JLabel. Once you've scaled the image to the size you want, the JLabel will take the size of the ImageIcon.

Read the picture as a BufferedImage

BufferedImage img = null;
try {
    img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
    e.printStackTrace();
}

Resize the BufferedImage

Image dimg = img.getScaledInstance(label.getWidth(), label.getHeight(),
        Image.SCALE_SMOOTH);

Make sure that the label width and height are the same proportions as the original image width and height. In other words, if the picture is 600 x 900 pixels, scale to 100 X 150. Otherwise, your picture will be distorted.

Create an ImageIcon

ImageIcon imageIcon = new ImageIcon(dimg);
Abdul Saleem
  • 10,098
  • 5
  • 45
  • 45
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • If the JLabel resizes with the window, can I assign some callback for a resize event, so that images will not overflow when the window becomes smaller? – Tomáš Zato Jan 02 '14 at 20:32
  • @Tomáš Zato: Yes, you can monitor the size of the JFrame with a [ComponentListener](http://stackoverflow.com/questions/2106367/listen-to-jframe-resize-events-as-the-user-drags-their-mouse). – Gilbert Le Blanc Jan 07 '14 at 07:24
  • 9
    When I tried this, the `img.getScaledInstance` result was unable to be assigned to `BufferedImage` because it was an `Image` object. Also are `label.width` and `label.height` simply placeholders for integers? – intcreator Dec 04 '15 at 00:24
  • I changed the BufferedImage dimg = img.getScaledInstance(..) to Image dimg = img.getScaledInstance() to fix the issue mentioned in comments above. – zdenekca Jan 22 '17 at 23:20
  • I get this java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage – Aylian Craspa Jan 31 '17 at 17:40
  • 5
    the `getScaledInstance` function will maintain the aspect ratio of the image when you pass a negative value for either width or height. – Damien Plumettaz Mar 08 '17 at 09:23
  • @DamienPlumettaz you're the MVP – Joaquín L. Robles Feb 21 '18 at 13:11
  • Once I have the ImageIcon, I cannot add it to the JLabel. Wrong type. – Falsoon Jul 05 '20 at 14:17
  • Exception in thread "main" java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero – Peter Palmer May 25 '22 at 15:46
  • `img.getScaledInstance( ..., Image.SCALE_SMOOTH )` produces a _way better_ result for me on OpenJDK 17 (Windows 10) than complex `Graphics2D` solutions with sophisticated `RenderingHints`. I'm just not sure if that might depend on the platform (JRE and OS). – MrSnrub Jul 18 '23 at 01:50
38

You can try it:

ImageIcon imageIcon = new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
label.setIcon(imageIcon);

Or in one line:

label.setIcon(new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));

The execution time is much more faster than File and ImageIO.

I recommend you to compare the two solutions in a loop.

tirz
  • 2,041
  • 1
  • 22
  • 37
6
public static void main(String s[]) 
  {

    BufferedImage image = null;
    try 
    {
        image = ImageIO.read(new File("your image path"));

    } catch (Exception e) 
    {
        e.printStackTrace();
    }

    ImageIcon imageIcon = new ImageIcon(fitimage(image, label.getWidth(), label.getHeight()));
    jLabel1.setIcon(imageIcon);
}


private Image fitimage(Image img , int w , int h)
{
    BufferedImage resizedimage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = resizedimage.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(img, 0, 0,w,h,null);
    g2.dispose();
    return resizedimage;
}
glee8e
  • 6,180
  • 4
  • 31
  • 51
amr foda
  • 69
  • 1
  • 1
5

The best and easy way for image resize using Java Swing is:

jLabel.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("/res/image.png")).getImage().getScaledInstance(200, 50, Image.SCALE_SMOOTH)));

For better display, identify the actual height & width of image and resize based on width/height percentage

Renjith Thomas
  • 147
  • 1
  • 4
3

i have done the following and it worked perfectly

try {
        JFileChooser jfc = new JFileChooser();
        jfc.showOpenDialog(null);
        File f = jfc.getSelectedFile();
        Image bi = ImageIO.read(f);
        image1.setText("");
        image1.setIcon(new ImageIcon(bi.getScaledInstance(int width, int width, int width)));

    } catch (Exception e) {
    } 
2

Or u can do it this way. The function u put the below 6 lines will throw an IOException. And will take your JLabel as a parameter.

BufferedImage bi=new BufferedImage(label.width(),label.height(),BufferedImage.TYPE_INT_RGB);

Graphics2D g=bi.createGraphics();

Image img=ImageIO.read(new File("path of your image"));

g.drawImage(img, 0, 0, label.width(), label.height(), null);

g.dispose();

return bi;
Java Man
  • 1,854
  • 3
  • 21
  • 43
2
public void selectImageAndResize(){    
    int returnVal = jFileChooser.showOpenDialog(this); //open jfilechooser
    if (returnVal == jFileChooser.APPROVE_OPTION) {    //select image
        File file = jFileChooser.getSelectedFile();    //get the image
        BufferedImage bi;
        try {
            //
            //transforms selected file to buffer
            //
            bi=ImageIO.read(file);  
            ImageIcon iconimage = new ImageIcon(bi);

            //
            //get image dimensions
            //
            BufferedImage bi2 = new BufferedImage(iconimage.getIconWidth(), iconimage.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 
            Graphics g = bi.createGraphics();
            iconimage.paintIcon(null, g, 0,0);
            g.dispose();

            //
            //resize image according to jlabel
            //
            BufferedImage resizedimage=resize(bi,jLabel2.getWidth(), jLabel2.getHeight()); 
            ImageIcon resizedicon=new ImageIcon(resizedimage);
            jLabel2.setIcon(resizedicon);
        } catch (Exception ex) {
            System.out.println("problem accessing file"+file.getAbsolutePath());
        }
    }
    else {
        System.out.println("File access cancelled by user.");
    }
}
  • 1
    Some words of explanation usually are appreciated on stack overflow. – mkl Sep 12 '17 at 09:37
  • you can still edit your post and add some explanations. You find the [edit link](https://stackoverflow.com/posts/46172451/edit) right under your answer post. – mkl Sep 13 '17 at 04:20
0

Assign your image to a string. Eg image Now set icon to a fixed size label.

image.setIcon(new javax.swing.ImageIcon(image.getScaledInstance(50,50,WIDTH)));
Peter Alsen
  • 320
  • 2
  • 5
  • 15
muabal
  • 21