Here you can find the source of getInputStreamFromImage(Image imagen, String format)
public static InputStream getInputStreamFromImage(Image imagen, String format)
//package com.java2s; //License from project: Open Source License import java.awt.*; import java.awt.image.*; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import javax.imageio.ImageIO; import javax.swing.*; public class Main { /**//w w w . j a v a 2s . c om * Obtiene un objeto InputStream a partir de un objeto Image */ public static InputStream getInputStreamFromImage(Image imagen, String format) { BufferedImage bi = toBufferedImage(imagen); ByteArrayOutputStream buffer_img = new ByteArrayOutputStream(); try { ImageIO.write(bi, format, buffer_img); } catch (Exception e) { throw new RuntimeException(e); } byte[] bytes = buffer_img.toByteArray(); return new ByteArrayInputStream(bytes); } /** * Convierte un objeto Image a un BufferedImage * * @param img El objeto Image a convertir * @return El BufferedImage obtenido */ public static BufferedImage toBufferedImage(Image img) { if (img instanceof BufferedImage) { return (BufferedImage) img; } // Crea un buffered image con trasparencia BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB); // Dibuja la imagen en el buffered image Graphics2D bGr = bimage.createGraphics(); bGr.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); bGr.drawImage(img, 0, 0, null); bGr.dispose(); // Retorna el buffered image return bimage; } /** * Obtiene el ancho de una Image */ public static int getWidth(Image imagen) { int width = new ImageIcon(imagen).getIconWidth(); return width; } /** * Obtiene el ancho de un IconImage */ public static int getWidth(ImageIcon imagen) { return imagen.getIconWidth(); } /** * Obtiene el alto de una Image */ public static int getHeight(Image imagen) { int height = new ImageIcon(imagen).getIconHeight(); return height; } /** * Obtiene el alto de un IconImage */ public static int getHeight(ImageIcon imagen) { return imagen.getIconHeight(); } }