Convert an Image to a BufferedImage

From CodeCodex

Implementations

Java

import java.awt.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

public class ImageUtils {

  public static BufferedImage imageToBufferedImage(Image im) {
     BufferedImage bi = new BufferedImage
        (im.getWidth(null),im.getHeight(null),BufferedImage.TYPE_INT_RGB);
     Graphics bg = bi.getGraphics();
     bg.drawImage(im, 0, 0, null);
     bg.dispose();
     return bi;
  }
  
  public static BufferedImage readImageFromFile(File file) 
    throws IOException 
  {
     return ImageIO.read(file);
  }
  
  public static void writeImageToPNG
    (File file,BufferedImage bufferedImage) 
       throws IOException
  {
     ImageIO.write(bufferedImage,"png",file);
  }
  
  public static void writeImageToJPG
    (File file,BufferedImage bufferedImage) 
       throws IOException
  {
     ImageIO.write(bufferedImage,"jpg",file);
  }
}

You need to do something to transform an Image to BufferedImage. But since BufferedImage extends Image, so there isn't a back-conversion, it's not needed.