1

I want to make an image smaller using the following java code. The original image is show on the screen (height larger than maxHeight). Why?

try{

        final int maxHeight = 600;
        JLabel picLabel;
        BufferedImage myPicture = ImageIO.read(new File("src/VIZ/"+listOfFiles[1].getName()));
        int height = myPicture.getHeight();
        int width = myPicture.getWidth();
        if ( height < maxHeight ){
            int multiplier = height/maxHeight;
            height = maxHeight;
            int newWidth = width/multiplier;
            BufferedImage bi = new BufferedImage(newWidth, maxHeight, BufferedImage.TYPE_INT_ARGB);
            picLabel = new JLabel(new ImageIcon( bi ));
        }else{
        picLabel = new JLabel(new ImageIcon( myPicture ));
        }


        console.add(picLabel);
    }

    catch(Exception e){

    }
Ishikawa
  • 177
  • 2
  • 12
  • 1
    What is the value of `maxHeight`? also `BufferedImage bi = new BufferedImage(...)` will not work since you did not copy the image data to the `bi` instance – iTech Feb 16 '13 at 20:29
  • 1
    +1 to @iTech answer. See [this](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14550112#14550112) similar question/answer and its [expanded variation](http://stackoverflow.com/questions/12660122/image-resizing-and-displaying-in-a-jpanel-or-a-jlabel-without-loss-of-quality/12660146#12660146). Also it seems you want to maintain aspect ratio of image. See [this](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959913#11959913) similar question/answer too – David Kroukamp Feb 16 '13 at 20:33
  • See also this [Q&A](http://stackoverflow.com/q/6916693/230513). – trashgod Feb 16 '13 at 22:56

0 Answers0