SWT透明像素变为白色

时间:2017-11-28 17:29:45

标签: java image swt

我尝试创建透明图像,然后在其上绘制包含透明/半透明像素的另一个图像。在添加图像之后,与颜色和α的混合相比,半透明像素被设置为白色。这导致图像周围出现白色轮廓。

图像必须在ToolBar上显示为具有透明像素的ToolItem。

是否可以在不将透明像素更改为白色的情况下绘制具有透明度的图像?

叠加图片

Overlay Image

已处理图片

Processed Image

final Display display = new Display();
final Shell shell = new Shell(display);
shell.setSize(285,305);

// Create a transparent image
final Image transparentImage = new Image(null, 256, 256);
final ImageData imageData = transparentImage.getImageData();
imageData.transparentPixel = imageData.getPixel(0, 0);
transparentImage.dispose();

// Create an image with the transparent data
final Image processedImage = new Image(Display.getCurrent(),
        imageData);

// Get the image to draw onto the transparent image
final Image overlayImage = new Image(display, "overlay_image.png");

final GC imageGC = new GC(processedImage);

imageGC.setAntialias(SWT.ON);

imageGC.drawImage(overlayImage, 0, 0);

imageGC.dispose();

// Create the components and add the image
final ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.BORDER);

final ToolItem item = new ToolItem(toolBar, SWT.NONE);
item.setImage(processedImage);

toolBar.pack();
shell.open();
while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
    display.sleep();
}

overlayImage.dispose();
display.dispose();

1 个答案:

答案 0 :(得分:0)

问题出在你使用的图像上,它有一个透明的白色,所以如果你想使用那个图像,或者你改变用程序(GIMP?)操纵图像的透明色,或者你可以给它两个图像都是透明的颜色,所以首先从图像中取出透明色" overlay_image.png"在位置(0,0)并且仅改变为第二(透明)图像设置相同的透明颜色。这是带有您图像的工作代码:

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class TestTransparency {


    public static void main(String[] args) {


        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setSize(285,305);


        final Image overlayImage = new Image(display, "overlay_image.png");
        final ImageData imageDataOverlay = overlayImage.getImageData();
        imageDataOverlay.transparentPixel = imageDataOverlay.transparentPixel;


        // Create a transparent image
        final Image transparentImage = new Image(display, 800, 600);
        final ImageData imageData = transparentImage.getImageData();
        imageData.transparentPixel = imageDataOverlay.transparentPixel;
        transparentImage.dispose();
        final Image processedImage = new Image(Display.getCurrent(),
                imageData);



        final Image overlayImage2 = new Image(Display.getCurrent(),
                imageDataOverlay);
        final GC imageGC = new GC(processedImage);
        imageGC.setAntialias(SWT.ON);
        imageGC.drawImage(overlayImage2, 0, 0);
        imageGC.dispose();

        // Create the components and add the image
        final ToolBar toolBar = new ToolBar(shell, SWT.FLAT | SWT.BORDER);
        toolBar.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
        final ToolItem item = new ToolItem(toolBar, SWT.NONE);
        item.setImage(processedImage);

        toolBar.pack();
        shell.open();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }

        overlayImage.dispose();
        display.dispose();



    }

}

请注意,使用GIMP操作图像:

enter image description here

或使用这样的良好透明图像:http://www.stickpng.com/img/icons-logos-emojis/tech-companies/youtube-play-logo您的代码工作正常!