Java Image Resize getResizedImage(Image originalImage, Dimension newSize)

Here you can find the source of getResizedImage(Image originalImage, Dimension newSize)

Description

Get a resized image.

License

Open Source License

Parameter

Parameter Description
originalImage the original image
newSize Dimension the size to which to pad/crop.

Return

BufferedImage the resulting image.

Declaration

public static BufferedImage getResizedImage(Image originalImage,
        Dimension newSize) 

Method Source Code

//package com.java2s;

import java.awt.Dimension;

import java.awt.Graphics2D;
import java.awt.Image;

import java.awt.image.BufferedImage;

public class Main {
    /**//  w ww .j  a v  a2 s. c o  m
     * Get a resized image.
     * The original image will appear in the resized image, but will be padded or cropped as appropriate.
     * @param originalImage the original image
     * @param newSize Dimension the size to which to pad/crop.
     * @return BufferedImage the resulting image.
     */
    public static BufferedImage getResizedImage(Image originalImage,
            Dimension newSize) {
        // construct the buffered image
        BufferedImage bImage = new BufferedImage(newSize.width,
                newSize.height, BufferedImage.TYPE_INT_ARGB);

        // obtain its graphics
        Graphics2D g2d = bImage.createGraphics();

        // draw the original image into the new BufferedImage.
        g2d.drawImage(originalImage, null, null);

        // dispose the graphics object
        g2d.dispose();

        return bImage;
    }
}

Related

  1. getResize(Image image, Dimension windowSize)
  2. getResizedImage(Image img, int iNewWidth, int iNewHeight)
  3. getResizedImage(Image original, final Integer w, final Integer h, boolean keepAspectRatio)
  4. getResizedImage(Image originalImage, int newWidth, int newHeight, int imageType)
  5. getResizedImage(String path, int height, int width)
  6. resize(Image img, int newWidth, float quality)
  7. resize(String srcImageFile, String result, int newWidth, float quality)