Reputation: 6197
Im displaying .png image with transparencity which works well by default... until I resize the image:
public Image resize(Image image, int width, int height)
{
Image scaled = new Image(Display.getDefault(), width, height);
scaled.getImageData().transparentPixel = image.getImageData().transparentPixel;
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();
return scaled;
}
then the transparency is gone, I can see the "white" color
Upvotes: 1
Views: 299
Reputation: 111142
getImageData()
gives you a copy of the image data so setting the transparent pixel on that does not change the original image.
Instead you need to create another image from the scaled image data with the transparent pixel set. So something like:
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 data from scaled image and transparent pixel from original
ImageData imageData = scaled.getImageData();
imageData.transparentPixel = image.getImageData().transparentPixel;
// Final scaled transparent image
Image finalImage = new Image(Display.getDefault(), imageData);
scaled.dispose();
Note that this only works when the original image data has the same format as the scaled image data (particularly the palette data)
If the data is in a different format use:
ImageData origData = image.getImageData();
imageData.transparentPixel = imageData.palette.getPixel(origData.palette.getRGB(origData.transparentPixel));
Upvotes: 2