Java ImageIcon Resize resizeImageAspectRatio(int width, int height, ImageIcon orig_img)

Here you can find the source of resizeImageAspectRatio(int width, int height, ImageIcon orig_img)

Description

Resize image aspect ratio.

License

Open Source License

Parameter

Parameter Description
width the width
height the height
orig_img the orig_img

Return

the image

Declaration

public static Image resizeImageAspectRatio(int width, int height, ImageIcon orig_img) 

Method Source Code

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

import java.awt.Image;

import javax.swing.ImageIcon;

public class Main {
    /**//from   w w w .ja  v a  2  s . com
     * Resize image aspect ratio.
     * 
     * @param width
     *            the width
     * @param height
     *            the height
     * @param orig_img
     *            the orig_img
     * 
     * @return the image
     */
    public static Image resizeImageAspectRatio(int width, int height, ImageIcon orig_img) {
        Image img = null;
        float disp_aspect_ratio = (height * 1.0F) / width;
        float aspect_ratio = (orig_img.getIconHeight() * 1.0F) / orig_img.getIconWidth();
        if (aspect_ratio == disp_aspect_ratio) {
            img = orig_img.getImage().getScaledInstance(width, height, 0);
        } else if (aspect_ratio < disp_aspect_ratio) {
            // landscape
            img = orig_img.getImage().getScaledInstance(width, (int) (height * aspect_ratio / disp_aspect_ratio),
                    0);
        } else {
            // potrait
            img = orig_img.getImage().getScaledInstance((int) (width * disp_aspect_ratio / aspect_ratio), height,
                    0);
        }
        return img;
    }
}

Related

  1. getResizedImageIcon(ImageIcon sourceImageIcon, int iRequiredWidth, int iRequiredHeight)
  2. resize(ImageIcon icon, int extent)
  3. resize(ImageIcon image, int width, int height)
  4. resize(ImageIcon src, Dimension size)
  5. resize(ImageIcon src, int destWidth, int destHeight)
  6. resizeImageIcon(ImageIcon icon, int width, int height)
  7. to50x50(ImageIcon imageIcon)