我有一个从PNG加载的org.eclipse.swt.graphics.Image,并希望以高质量(抗锯齿,插值)对其进行缩放。 但是我不想失去透明度,而只是获得白色背景。 (我需要将此图像放在org.eclipse.swt.widgets.Label上。)

有人知道该怎么做吗? 谢谢!

===============>>#1 票数:1 已采纳

使用Sean Bright在此处描述的方法: https : //stackoverflow.com/a/15685473/6245535 ,我们可以从图像中提取Alpha信息,并使用它来填充负责透明度的ImageData.alphaData数组:

public static Image resizeImage(Display display, Image image, int width, int height) {      Image scaled = new Image(display, 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();      ImageData canvasData = scaled.getImageData();     canvasData.alphaData = new byte[width * height];      // This is the hacky bit that is making assumptions about     // the underlying ImageData. In my case it is 32 bit data     // so every 4th byte in the data array is the alpha for that     // pixel...     for (int idx = 0; idx < (width * height); idx++) {         int coord = (idx * 4) + 3;         canvasData.alphaData[idx] = canvasData.data[coord];     }      // Now that we've set the alphaData, we can create our     // final image     Image finalImage = new Image(display, canvasData);      scaled.dispose();      return finalImage; } 

请注意,此方法假定您使用的是32位深度的颜色。 否则将无法正常工作。

  ask by Granini translate from so

本文未有回复,本站智能推荐: