Java,SWT,图像透明度在调整大小时丢失

Java,SWT,图像透明度在调整大小时丢失,java,swt,Java,Swt,即时消息显示.png图像的透明度,默认情况下效果良好。。。在调整图像大小之前: public Image resize(Image image, int width, int height) { Image scaled = new Image(Display.getDefault(), width, height); scaled.getImageData().transparentPixel = image.getImageData().transparentPixel;

即时消息显示.png图像的透明度,默认情况下效果良好。。。在调整图像大小之前:

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;
}
然后透明度消失,我可以看到“白色”颜色

getImageData()
提供图像数据的副本,因此设置透明像素不会改变原始图像

相反,您需要使用透明像素集从缩放图像数据创建另一个图像。比如:

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();
请注意,这仅在原始图像数据与缩放图像数据(尤其是调色板数据)具有相同格式时才起作用

如果数据的格式不同,请使用:

ImageData origData = image.getImageData();

imageData.transparentPixel = imageData.palette.getPixel(origData.palette.getRGB(origData.transparentPixel));

很抱歉,它不工作,透明度仍然没有(并且gc.dispose()应该转到最后一行,因为仍然使用gs)可能图像数据格式不同,在这种情况下,计算透明像素的值比较困难。尝试类似于
imageData.transparentPixel=imageData.getPixel(0,0)的方法
使用左上角的像素作为透明颜色。添加了一些代码,我认为如果图像格式不同,应该可以使用这些代码