Java Graphics How to - Convert Raster to RenderedImage








Question

We would like to know how to convert Raster to RenderedImage.

Answer

import java.awt.image.BufferedImage;
import java.io.File;
/*from  ww w  .j  a va 2  s  .c om*/
import javax.imageio.ImageIO;

public class Main {

  public static void main(String[] args) throws Exception {
    BufferedImage original = new BufferedImage(100, 100,
        BufferedImage.TYPE_3BYTE_BGR);

    BufferedImage copy = original.getSubimage(10, 10, 50, 50);

    ImageIO.write(copy, "png", new File("Test.png"));
  }

}