Quickly zooming a BufferedImage

      No Comments on Quickly zooming a BufferedImage

If you don’t need a scaled instance of your BufferedImage in the memory, you can use “on-the-fly scaling”. For example, this custom JComponent can be used to display a BufferedImage and zoom it with the setScaleFactor(..)-method

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import javax.swing.JComponent;

public class ZoomableImage extends JComponent {

    private float scaleFactor;
    private BufferedImage originalImage;

    public ZoomableImage() {
        this.scaleFactor = 1;
        this.setSize(0, 0);

    public void setImage(BufferedImage image) {
        this.originalImage = image;
        this.setSize(image.getWidth(), image.getHeight());
        //setSize does repainting, no need to call repaint()

    public void setScaleFactor(float scaleFactor) {
        this.scaleFactor = scaleFactor;

    public void paintComponent(Graphics g) {
        if (this.originalImage != null) {
            Graphics2D g2 = (Graphics2D) g;
            int newW = (int) (originalImage.getWidth() * scaleFactor);
            int newH = (int) (originalImage.getHeight() * scaleFactor);
            this.setPreferredSize(new Dimension(newW, newH));
            g2.drawImage(originalImage, 0, 0, newW, newH, null);

For some benchmarking on the different RenderingHints values see Chris Campbell’s great article The Perils of Image.getScaledInstance()


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.