Create a BufferedImage from an Image object in Java

Description

The following code shows how to create a BufferedImage from an Image object.

Example


   //w ww .  ja v a  2s  .c om


import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class Main {
  public static void main(String[] argv) throws Exception {
    Image  img = new ImageIcon("test.png").getImage();

    BufferedImage bufferedImage = new BufferedImage(img.getWidth(null), img.getHeight(null),
        BufferedImage.TYPE_INT_RGB);

    Graphics g = bufferedImage.createGraphics();
    g.drawImage(img, 0, 0, null);
    g.dispose();

    ImageIO.write(bufferedImage, "png", new File("a.png"));
  }
}




















Home »
  Java Tutorial »
    Graphics »




Animation
BufferedImage
Color
Font
Gradient
Graphics Settings
Image
Mouse Draw
Print
Shape
Text
Transform