Java examples for 2D Graphics:BufferedImage Resize
resize BufferedImage Maintaining Ratio
//package com.java2s; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.image.BufferedImage; public class Main { public static BufferedImage resizeMaintainingRatio(BufferedImage img, Dimension newSize) {//from www . j a va2 s. c om double w = img.getWidth(); double h = img.getHeight(); if (newSize.getWidth() != 0 && newSize.getHeight() != 0) { double ratioH = h / w; double ratioW = w / h; double newH, newW; newH = newSize.getHeight(); newW = newH * ratioW; if (newW > newSize.getWidth()) { newW = newSize.getWidth(); newH = newW * ratioH; } return resize(img, new Dimension((int) newW, (int) newH)); } else { return img; } } public static BufferedImage resize(BufferedImage img, Dimension newSize) { int type = img.getType(); if (type == 0) type = BufferedImage.TYPE_INT_ARGB; BufferedImage resizedImage = new BufferedImage( (int) newSize.getWidth(), (int) newSize.getWidth(), type); Graphics2D g = resizedImage.createGraphics(); g.drawImage(img, 0, 0, (int) newSize.getWidth(), (int) newSize.getWidth(), null); g.dispose(); return resizedImage; } }