2017-11-28 19 views
1

透明イメージを作成し、透明/半透明ピクセルを含む別のイメージを描画しようとしています。画像が追加された後、半透明のピクセルは、色とアルファの混合であるのに比べて白に設定されます。これにより、画像の周りに白い輪郭ができます。SWT透明ピクセルが白に変更されました

画像は透明なピクセルを持つToolItemとしてToolBarに表示する必要があります。

透過ピクセルを白に変更せずに透明画像を描画することはできますか?

オーバーレイ画像

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(); 

答えて

0

問題は、それが白透明色を持って、あなたが使用するイメージであるので、あなたが私たちに欲しいなら(GIMP?)を使って画像を操作する透明色を変更したり、両方の画像に同じ透明色を与えることができるので、最初に画像の透明色を "overlay_image.png"の位置( 0,0)のみ変更し、2番目の(透過的な)画像に同じ透明色を設定します。これはあなたのイメージで作業コードです:

enter image description here

またはこのような良い透明な画像を使用して::http://www.stickpng.com/img/icons-logos-emojis/tech-companies/youtube-play-logoあなたのコードが正常に動作

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を使用して、ご注意ください!

関連する問題