Search code examples
javaswingjlabelimageicon

How to scale image using getScaledImage?


This is what i have tried to scale image using getScaledInstance(). But its not scaling the image. Can anybody correct this code?

BufferedImage image = ImageIO.read(new File("img.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(image));
image=(BufferedImage)image.getScaledInstance(50,50, Image. SCALE_SMOOTH);
add(picLabel);

Solution

  • I done this and now its working fine. :)

        BufferedImage image = ImageIO.read(new File("img.jpg"));
        BufferedImage img = new BufferedImage(200,150,BufferedImage.TYPE_INT_RGB);
        img.getGraphics().drawImage(image,0,0,200,150,null);
    
        JLabel label = new JLabel(new ImageIcon(img));
        add(label);