Java BufferedImage Subimage getSubimage(BufferedImage image, int x, int y, int width, int height)

Here you can find the source of getSubimage(BufferedImage image, int x, int y, int width, int height)

Description

get Subimage

License

Open Source License

Exception

Parameter Description
IllegalArgumentException an exception

Declaration


public static BufferedImage getSubimage(BufferedImage image, int x, int y, int width, int height) 

Method Source Code

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

import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class Main {
    /**/*from   ww w.j  a  va  2 s. c o m*/
     * @throws IllegalArgumentException
     */

    public static BufferedImage getSubimage(BufferedImage image, int x, int y, int width, int height) {
        // Validate arguments
        if ((x < 0) || (width < 0) || (x + width > image.getWidth()) || (y < 0) || (height < 0)
                || (y + height > image.getHeight()))
            throw new IllegalArgumentException();

        // Create a new image from a region of the input image
        BufferedImage subimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        int[] rgbBuffer = new int[width * height];
        image.getRGB(x, y, width, height, rgbBuffer, 0, width);
        subimage.setRGB(0, 0, width, height, rgbBuffer, 0, width);
        return subimage;
    }

    /**
     * @throws IllegalArgumentException
     */

    public static BufferedImage getSubimage(BufferedImage image, Rectangle rect) {
        return getSubimage(image, rect.x, rect.y, rect.width, rect.height);
    }
}

Related

  1. getSubImage(BufferedImage image, int x, int y, int w, int h)
  2. getSubimage(BufferedImage image, int x, int y, int w, int h)
  3. getSubImage(BufferedImage image, int x, int y, int width, int height)
  4. getSubimage(BufferedImage image, int x, int y, int width, int height)
  5. getSubImage(BufferedImage img, int x, int y, int w, int h)
  6. getSubImage(BufferedImage img, Rectangle area)