I'm trying to cut a certain part of image in Java and save it back to disk. Is there a function that cuts the images from X, Y with the specified width and height?
You'd typically
BufferedImage
(dst
below) with the desired width and height.Graphics
objectsrc
below)BufferedImage
ImageIO
.In code:
Image src = ImageIO.read(new File("duke.jpg"));
int x = 10, y = 20, w = 40, h = 50;
BufferedImage dst = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
dst.getGraphics().drawImage(src, 0, 0, w, h, x, y, x + w, y + h, null);
ImageIO.write(dst, "png", new File("duke_cropped.png"));
Given this .jpg...
...It generates this .png:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With