2017-11-28 1 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

문제가 흰색 투명한 색상을 가지고, 당신이 사용하는 이미지, 그래서 네가 우리에게 원한다면 (김프?)로 투명 색상을 바꾸거나 두 이미지에 동일한 투명 색상을 줄 수 있으므로 먼저 "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 코드가 잘 작동!

관련 문제