Java Image to BufferedImage imageToBufferedImage(Image img)

Here you can find the source of imageToBufferedImage(Image img)

Description

Converts a given Image into a BufferedImage

License

Open Source License

Parameter

Parameter Description
img The Image to be converted

Return

The converted BufferedImage

Declaration

public static BufferedImage imageToBufferedImage(Image img) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*from  ww  w.j ava2s .  c o m*/
     * Converts a given Image into a BufferedImage
     * 
     * @param img
     *            The Image to be converted
     * @return The converted BufferedImage
     */
    public static BufferedImage imageToBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }

        // Create a buffered image with transparency
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null),
                BufferedImage.TYPE_INT_ARGB);

        // Draw the image on to the buffered image
        Graphics2D bGr = bimage.createGraphics();
        bGr.drawImage(img, 0, 0, null);
        bGr.dispose();

        // Return the buffered image
        return bimage;
    }
}

Related

  1. getBufferedImage(java.awt.Image image)
  2. getScaledBufferedImage(Image icon, double scale)
  3. imageToBufferedImage(Image image)
  4. ImageToBufferedImage(Image image, int width, int height)
  5. imageToBufferedImage(Image img)
  6. imageToBufferedImage(Image pImage)
  7. imageToBufferedImage(Image src)
  8. makeBufferedImage(final Image image)
  9. makeBufferedImage(Image image)