Java BufferedImage Crop cropImage(BufferedImage image, int width, int height)

Here you can find the source of cropImage(BufferedImage image, int width, int height)

Description

Crops an image to a specified size.

License

Open Source License

Parameter

Parameter Description
image - the image to crop.
width - the width we want to crop to.
height - the height we want to crop to.

Return

the cropped image.

Declaration

public static BufferedImage cropImage(BufferedImage image, int width, int height) 

Method Source Code


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

import java.awt.Graphics;

import java.awt.image.BufferedImage;

public class Main {
    /**/*from  ww w  . jav  a  2 s .co m*/
     * Crops an image to a specified size.
     *
     * @param image - the image to crop.
     * @param width - the width we want to crop to.
     * @param height - the height we want to crop to.
     * @return the cropped image.
     */
    public static BufferedImage cropImage(BufferedImage image, int width, int height) {
        if (width > 0 && height > 0) {
            BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics g = result.getGraphics();
            g.drawImage(image, 0, 0, width, height, null);
            g.dispose();

            // Retuned the new scaled and cropped image.
            return result;
        }

        return image;
    }
}

Related

  1. cropBoundingBox(Rectangle r, int width, int height)
  2. cropImage(BufferedImage image, int cropWidth, int cropHeight)
  3. cropImage(BufferedImage image, int fromX, int fromY, int width, int height)
  4. cropImage(BufferedImage image, int lc, int rc, int tc, int bc)
  5. cropImage(BufferedImage image, int width, int height)
  6. cropImage(BufferedImage image, int x, int y, int width, int height)
  7. cropImage(BufferedImage image, int x1, int y1, int x2, int y2)
  8. cropImage(BufferedImage src, int x, int y, int w, int h)
  9. cropImage(BufferedImage src, int x, int y, int w, int h)