Java Image Scale getScaledImage(Image srcImg, int w, int h)

Here you can find the source of getScaledImage(Image srcImg, int w, int h)

Description

Resizes an image using a Graphics2D object backed by a BufferedImage.

License

Open Source License

Parameter

Parameter Description
srcImg - source image to scale
w - desired width
h - desired height

Return

- the new resized image

Declaration

public static ImageIcon getScaledImage(Image srcImg, int w, int h) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

public class Main {
    /**/*from w w  w.j a va2 s  . c  o m*/
     * Resizes an image using a Graphics2D object backed by a BufferedImage.
     *
     * @param srcImg - source image to scale
     * @param w - desired width
     * @param h - desired height
     * @return - the new resized image
     */
    public static ImageIcon getScaledImage(Image srcImg, int w, int h) {
        BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = resizedImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(srcImg, 0, 0, w, h, null);
        g2.dispose();
        return new ImageIcon(resizedImg);
    }
}

Related

  1. getScaledIcon(final Image image, final double scale)
  2. getScaledImage(final ImageIcon icon, final int newMaxWidth, final int newMaxHeight, final boolean maintainRatio)
  3. getScaledImage(Image image, int maxWidth, int maxHeight)
  4. getScaledImage(Image imagen, double scaleW, double scaleH)
  5. getScaledImage(ImageIcon icon, int w, int h)
  6. getScaledImage(ImageIcon srcImg, int h, int maxWidth)
  7. getScaleToFit(JPanel componentToFit, File imageFile)
  8. scaleImage(Image image, float scale)