2

I am trying to load an image from directory and place it as an icon in JLabel. But when the image size is big it doesn't fit completely in the label. I tried resizing the image to fit in the label but its not working. May I know where I am going wrong?

JFileChooser choose=new JFileChooser();
choose.showOpenDialog(null);
File f=choose.getSelectedFile();
String filename=f.getAbsolutePath();
path.setText(filename);

ImageIcon icon=new ImageIcon(filename);
Image img=icon.getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null),   BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img,500, 500, null);
ImageIcon newIcon = new ImageIcon(bi);
image_label.setIcon(newIcon);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
jayesh.doolani
  • 39
  • 1
  • 10
  • Ahh, don't. Seriously. Instead try a define a "maximum" allowable size and down scale any images that are too large. For [example](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003) and [example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928) – MadProgrammer Mar 27 '14 at 06:30
  • *"where I am going wrong?"* - To start with, you create a `BufferedImage` whose size is the same as the image you just loaded, then paint the image 500x500 within the canvas...unless the image is greater then 500x500, the image will be rendered out of view... – MadProgrammer Mar 27 '14 at 06:32
  • @MadProgrammer I tried going through the 2 examples you gave, and I want to admit I am a newbie to dealing with images hence I couldn't understand what's going on. The size of my jLabel is 500x500 hence I am scaling the image to 500x500 and the image which I hav loaded is definitely bigger than that, – jayesh.doolani Mar 27 '14 at 06:40
  • You're not scaling it, `drawImage(Image, int, int, ImageObserver)` draws the image at the specified x/y position, it doesn't scale it... – MadProgrammer Mar 27 '14 at 06:41
  • @MadProgrammer BufferedImage img=null; try { img=ImageIO.read(f); BufferedImage dimg = (BufferedImage) img.getScaledInstance(500,500,Image.SCALE_SMOOTH); ImageIcon imageIcon = new ImageIcon(dimg); image_label.setIcon(imageIcon); How about this piece of code? – jayesh.doolani Mar 27 '14 at 06:43
  • That would be better, how ever, [The Perils of Image.getScaledInstance()](https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html) – MadProgrammer Mar 27 '14 at 06:48
  • @MadProgrammer When I try to implement my code I get an error "java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage" What's the reason? I am trying to cast the image to bufferedimage. If i don't cast then I get an error. – jayesh.doolani Mar 27 '14 at 06:53
  • `Image` isn't a type of `BufferedImage`, so you can't cast `Image` to `BufferedImage`, the code you have in the comment loads the image using `ImageIO` which returns a `BufferedImage`, why not just use that – MadProgrammer Mar 27 '14 at 06:57
  • @MadProgrammer When I only use BufferedImage like BufferedImage dimg = img.getScaledInstance(500,500,Image.SCALE_SMOOTH); I get an error saying incompatible types required BufferedImage found Image – jayesh.doolani Mar 27 '14 at 07:02
  • I think it's time you read the [JavaDocs](http://docs.oracle.com/javase/7/docs/api/java/awt/Image.html#getScaledInstance%28int,%20int,%20int%29), `getScaledInstance` returns `Image`, not `BufferedImage` – MadProgrammer Mar 27 '14 at 07:06
  • @MadProgrammer I got my answer, I changed the line to Image dimg = img.getScaledInstance(500,500,Image.SCALE_SMOOTH); and its working. – jayesh.doolani Mar 27 '14 at 07:06

1 Answers1

2
BufferedImage img = ImageIO.read(...);
Image scaled = img.getScaledInstance(500, 500, Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(scaled);

Beware, that this will scale the image so that it is square. Take a look at Java: maintaining aspect ratio of JPanel background image which discusses maintaining the aspect ratio of the image when scaled.

Also, you should read The Perils of Image.getScaledInstance() and have a look at Scale the ImageIcon automatically to label size which uses a divide and conqure scaling algorithim and Quality of Image after resize very low -- Java which demonstrates the issues of doing a one step scale...

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366