pbreault pbreault - 1 year ago 98
Java Question

How can I resize an image using Java?

I need to resize PNG, JPEG and GIF files. How can I do this using Java?

Answer Source

After loading the image you can try:

BufferedImage createResizedCopy(Image originalImage, 
    		int scaledWidth, int scaledHeight, 
    		boolean preserveAlpha)
    	int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    	BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    	Graphics2D g = scaledBI.createGraphics();
    	if (preserveAlpha) {
    	g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); 
    	return scaledBI;