private byte [ ] getPNGBytes ( Image img ) { RenderedImage ri ; if ( img instanceof RenderedImage ) { ri = ( RenderedImage ) img ; } else { BufferedImage bi = new BufferedImage ( img . getWidth ( null ) , img . getHeight ( null ) , BufferedImage . TYPE_INT_ARGB ) ; Graphics2D g2 = bi . createGraphics ( ) ; g2 . drawImage ( img , 0 , 0 , null ) ; ri = bi ; } ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; try { ImageIO . write ( ri , "png" , baos ) ; } catch ( IOException ex ) { Logger . getLogger ( SVGGraphics2D . class . getName ( ) ) . log ( Level . SEVERE , "IOException while writing PNG data." , ex ) ; } return baos . toByteArray ( ) ; }
Returns the bytes representing a PNG format image .