Twitter github

Resizing Images using SWT

I was toying with ECF today along with those new fancy custom tooltips.

In the process of doing this, I was dealing with images obtained from google’s GTalk server and they were coming back in crazy shapes and sizes. I thought to myself, it can’t be that hard to resize images, lo’ and behold… it really isn’t… here’s a code snippet to do it (may not be the best way, but it works):


private Image resize(Image image, int width, int height) {
Image scaled = new Image(Display.getDefault(), width, height);
GC gc = new GC(scaled);
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.drawImage(image, 0, 0,
image.getBounds().width, image.getBounds().height,
0, 0, width, height);
gc.dispose();
image.dispose(); // don't forget about me!
return scaled;
}

Now all I need to figure out is how to make those new fancy custom tooltips even better. I have a usecase where I want a user to click that tooltip, if so, leave the tooltip open and allow the user to click possible hyperlinks and close it manually later on. The reason I want to do this is because I want to allow people like the Mylyn folks maybe to plug something in where they could have a hyperlink to show relevant tasks on a user.

  • Tom
  • Tom
  • Csaba

    For resizing SWT images, we use ImageData.scaledTo. It is very fast and convenient.

  • Csaba

    For resizing SWT images, we use ImageData.scaledTo. It is very fast and convenient.

  • nricheton

    ImageData#scaledTo does not use interpolation, so resized images look bad most of the time.

    (I also blogged -in french- about image resizing a long time ago:
    http://nricheton.homeip.net/?p=6 πŸ™‚ )

  • nricheton

    ImageData#scaledTo does not use interpolation, so resized images look bad most of the time. (I also blogged -in french- about image resizing a long time ago:http://nricheton.homeip.net/?p=6 πŸ™‚ )

  • Tom

    I’m using standard java API BufferedImage + AffineTransform to scale images. Is using SWT-API better?

  • Tom

    I’m using standard java API BufferedImage + AffineTransform to scale images. Is using SWT-API better?

  • Mark

    Rule 1: If you created it, you dispose it.

    You probably shouldn’t dispose the original image in your function because it was passed in as a parameter. Prefer instead to let the function that created the image be responsible for disposing it.

  • Mark

    Rule 1: If you created it, you dispose it.You probably shouldn’t dispose the original image in your function because it was passed in as a parameter. Prefer instead to let the function that created the image be responsible for disposing it.

  • Sergey Nazarov

    This implementation doesn’t work with transparent background.
    I’ve tested on ubuntu 15.04

  • Sergey Nazarov

    This method doesn’t work for png with transparency. For png with transparency you shouldn’t create new Image instance, you should draw directly to canvas.
    Code Example:
    public static Canvas drawScaledImage(Image image, Composite parent, int width, int height) {
    Canvas imageCanvas = new Canvas (parent, SWT.NONE);
    imageCanvas.addPaintListener( (event) -> {
    GC gc = event.gc;
    gc.setAntialias(SWT.ON);
    gc.setInterpolation(SWT.HIGH);
    gc.drawImage(image, 0, 0, image.getBounds().width, image.getBounds().height, 0, 0, width, height);
    });

    return imageCanvas;
    }