Dec 03

There are two main ways to scale an image.

The first is to ‘paint’ a scaled version of the image to a new image of the required size.

// Create new (blank) image of required (scaled) size

BufferedImage scaledImage = new BufferedImage(
   width, height, BufferedImage.TYPE_INT_ARGB);

// Paint scaled version of image to new image

Graphics2D graphics2D = scaledImage.createGraphics();
graphics2D.drawImage(image, 0, 0, width, height, null);

// clean up


The second is to use an AffineTransform

BufferedImage scaledImage = new BufferedImage(
	width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics2D = scaledImage.createGraphics();
AffineTransform xform = AffineTransform.getScaleInstance(scale, scale);
graphics2D.drawImage(image, xform, null);

There’s also a getScaledInstance() method of the Image class which has been around since the early days of Java. Generally its worth avoiding for performance reasons.

written by objects \\ tags: ,

Array ( ) 13 Responses to “How do I scale or resize a BufferedImage?”

  1. Alan Says:

    you didn’t anwer the question. None of these create BufferedImages… they just draw scaled instances of them via grahics.

  2. objects Says:

    They create a new BufferedImage which is referenced by the variable scaledImage.

  3. Trevor Says:

    TYPE_INT_RGB should be TYPE_INT_ARGB. This adds support for images with transparent layers.

  4. objects Says:

    Thanks Trevor, good pickup. Have updated the code accordingly.

  5. MyJava Says:

    My Images get a kind of Red overground. Why it happens?

  6. objects Says:

    That’s strange, does it happen with all images?

  7. Ioan_informaticianu Says:

    because when u save on disk the image,the jpg format don’t have transparency. switch to TYPE_INT_RGB and it will work

  8. SciFi Junkie Says:

    Method 1 worked great. Thanks. Too bad you can’t just scale an image.

  9. Shaving Brushes Says:

    I am very thankful to this topic because it really gives great information

  10. tom Says:

    You might rather want to use the original image.getType() so you always get what you started with (TYPE_INT_RGB for jpg and TYPE_INT_ARGB for gif or png).

  11. Greg Says:

    The both do scale I believe. What about resize? To me resize means:
    1) make the image larger with the new pixels some default value
    2) make the image smaller by clobbering existing pixels

  12. objects Says:

    Good point. A resize would just involve creating a new image and directly copying in the pixels from the source.
    Will look to add an example.

  13. Alex Says:

    I am using the first version and everything is working perfectly on Windows, but when I run this on Unix (at least IBM AIX, I have not tested others), it will sometimes change the colors of the image. Sometimes the images get a pink or yellow overlay on them sometimes.

    Again, this only happens on Unix. It is working fine on Windows. Here is the code I am using that deals with scaling the image. Am I doing anything wrong here that would cause this to not work on Unix, or is there a better way of doing this?

    Thanks for your help

    int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    Graphics2D g = resizedImage.createGraphics();

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.drawImage(image, 0, 0, width, height, null);

Leave a Reply

You must be logged in to post a comment.