SWT从文件流InputStream创建Image

private void setBtnImage(Composite btnComposite)
    {
        Display display = btnComposite.getDisplay();

        addButton.setImage(new Image(display, this.getClass()
                .getClassLoader()
                .getResourceAsStream("icons//add.gif")));
        deleteButton.setImage(new Image(display, this.getClass()
                .getClassLoader()
                .getResourceAsStream("icons//delete.gif")));
        upButton.setImage(new Image(display, this.getClass()
                .getClassLoader()
                .getResourceAsStream("icons//up.gif")));
        downButton.setImage(new Image(display, this.getClass()
                .getClassLoader()
                .getResourceAsStream("icons//down.gif")));
    }

//getClass().getResourceAsStream("icons//delete.gif"))
className.class.getResourceAsStream("buttonImage.gif")
this.getClass().getClassLoader().getResourceAsStream("icons//add.gif")));

public Object nativeToJava(TransferData transferData){                 
    if (!isSupportedType(transferData)) return null;                   
                                                                       
    byte[] buffer = (byte[])super.nativeToJava(transferData);          
    if (buffer == null) return null;                                   
                                                                       
    ImageData imdata;                                                  
	      try {                                                          
        ByteArrayInputStream in = new ByteArrayInputStream(buffer);    
        DataInputStream readIn = new DataInputStream(in);              
        imdata = new ImageData(readIn);                                
        readIn.close();                                                
    } catch (IOException ex) {                                         
        return null;                                                   
    }                                                                  
    return imdata;                                                     
}              


你可能感兴趣的:(SWT/JFace)