Java,SET,调整大小时图像透明度丢失
Java, SWT, the image transparencity gets lost when resizing
在显示默认情况下效果很好的透明 .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;
}
然后透明度消失了,我可以看到"white"颜色
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));
在显示默认情况下效果很好的透明 .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;
}
然后透明度消失了,我可以看到"white"颜色
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));