Create a Buffered Image from an Image in Java

Description

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

Example


    /*from  w  w  w .  j a  va  2  s . c om*/



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

import javax.swing.ImageIcon;

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

  public static BufferedImage toBufferedImage(Image image) {
    image = new ImageIcon(image).getImage();

    BufferedImage bimage = new BufferedImage(image.getWidth(null), image
        .getHeight(null), BufferedImage.TYPE_INT_ARGB);

    Graphics g = bimage.createGraphics();

    g.drawImage(image, 0, 0, null);
    g.dispose();

    return bimage;
  }
}




















Home »
  Java Tutorial »
    Graphics »




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