Skip to content

Instantly share code, notes, and snippets.

@nicolasH
Created September 26, 2010 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolasH/598255 to your computer and use it in GitHub Desktop.
Save nicolasH/598255 to your computer and use it in GitHub Desktop.
//How to create a BufferedImage from a sub rectangle of another BufferedImage, a.k.a. 'clipping'.
BufferedImage src = ImageIO.read(new File("SomeFile.png"));
Rectangle clip = new Rectangle((src.getWidth() - 200) / 2, (src.getHeight() - 200) / 2, 200, 200);
BufferedImage ret = new BufferedImage(clip.width, clip.height, image.getType());
//short but slow clip and paste :
ret.getRaster().setRect(-clip.x, -clip.y, image.getData(clip));
//done !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment