May 23
// Load the image

image = new ImageIcon(image).getImage();

// Determine transparency for BufferedImage
// http://helpdesk.objects.com.au/java/how-to-determine-if-image-supports-alpha

boolean hasAlpha = hasAlpha(image);
int transparency = hasAlpha ? Transparency.BITMASK : Transparency.OPAQUE;

// Create the buffered image

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
BufferedImage bufferedImage = gc.createCompatibleImage(image.getWidth(null),
		image.getHeight(null), transparency);

if (bufferedImage == null) {
	
	// if that failed then use the default color model
	
	int type = hasAlpha ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB;
	bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
}

// Copy image

Graphics g = bufferedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();

written by objects \\ tags: , , ,


Leave a Reply